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

  • 相关阅读:
    java后台保存JSON
    查询树节点及其所有上级节点sql语句
    查询树节点及其所有下级节点sql语句
    Hibernate查询机制使用原生sql语法查询
    SSH框架通过poi导出excel表格
    java通过poi导入excel数据
    各类型日期date的相互转化
    推荐一下我喜欢的软件
    青岛市赛总结——远征石油大学
    My learn of git
  • 原文地址:https://www.cnblogs.com/yskcoder/p/4718226.html
Copyright © 2020-2023  润新知