• 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());
    }
    }

  • 相关阅读:
    SqlServer查询所有表名 查询表的所有列名
    IIS控制txt文件的访问
    CHM文件打不开
    [转]VS 2010项目中添加lib库
    SqlServer游标
    [转]_beginthread(), _beginthreadex()与CreateThread()的区别
    jquery radio快速度取值
    linx niginx下php无法创建文件夹及文件
    [转载]无效的 CurrentPageIndex 值.它必须大于等于 0 且小于 PageCount
    使用DateGrid的ItemCommand的一点心得
  • 原文地址:https://www.cnblogs.com/yskcoder/p/4718226.html
Copyright © 2020-2023  润新知