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

  • 相关阅读:
    leetcode 203
    vim插件管理器vundle
    centos7看电影
    getopt
    iOS/object-c: 枚举类型 enum,NS_ENUM,NS_OPTIONS
    "ALView+PureLayout.h"
    UIPageViewController教程
    (Mac ox 10.11+) CocoaPods安装,卸载,使用说明
    CocoaPods集成到Xcode项目中的步骤
    label_设置行距、字距及计算含有行间距的label高度
  • 原文地址:https://www.cnblogs.com/yskcoder/p/4718226.html
Copyright © 2011-2022 走看看