zoukankan      html  css  js  c++  java
  • java模拟post进行文件提交 采用httpClient方法

    package com.jd.vd.manage.util.filemultipart;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.nio.charset.Charset;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.entity.mime.content.StringBody;
    import org.apache.http.impl.client.DefaultHttpClient;

    public class Test {

    public static void main(String[] args) throws ClientProtocolException, IOException {
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,"--------------------HV2ymHFg03ehbqgZCaKO6jyH", Charset.defaultCharset());
    multipartEntity.addPart("ram",new StringBody("4654646", Charset.forName("UTF-8")));
    multipartEntity.addPart("key",new StringBody("123456",Charset.forName("UTF-8")));
    multipartEntity.addPart("from",new StringBody("cw",Charset.forName("UTF-8")));
    multipartEntity.addPart("file",new FileBody(new File("D:/upload/JDELOG.txt")));

    HttpPost request = new HttpPost("http://localhost:8080/sdkServlet");
    request.setEntity(multipartEntity);
    request.addHeader("Content-Type","multipart/form-data; boundary=--------------------HV2ymHFg03ehbqgZCaKO6jyH");

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response =httpClient.execute(request);
    InputStream is = response.getEntity().getContent();
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    StringBuffer buffer = new StringBuffer();
    String line = "";
    while ((line = in.readLine()) != null) {
    buffer.append(line);
    }
    System.out.println("发送消息收到的返回:"+buffer.toString());
    }
    }

  • 相关阅读:
    require笔札
    zepto_core
    2016年规划
    说说Q.js中的promise的历史
    jQuery.extend
    jQuery.core_02
    javascript之this指针
    javascript之闭包
    javascript之作用域链
    jvavascript之变量对象
  • 原文地址:https://www.cnblogs.com/yskcoder/p/4718226.html
Copyright © 2011-2022 走看看