# http上传json文件自带header参数

用http上传功能传参,目的是接收远端传过来的json文件,文件里面的内容及响应的请求header里面的内容。 下面是主要的代码:

JqcxResponse jqcxResponse=new JqcxResponse();
HttpPost post = new HttpPost(jqcxParameter.getUrl());//"http://wycapi.gzjt.gov.cn/api/app/common/binapi"
InputStream in = new ByteArrayInputStream(jqcxParameter.getFileContent().getBytes("UTF-8"));
String md5 = hash(in);
in.reset();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
post.addHeader("binfile-md5", md5);
post.addHeader("binfile-auth", jqcxParameter.getBinfileAuth());//"jiqiyc"
post.addHeader("binfile-gzip", jqcxParameter.getBinfileGzip());//false
post.addHeader("binfile-reqlen", String.valueOf(in.available()));
String filename = jqcxParameter.getFilename();//"jiqiyc_TOKEN_ACCESS_REQ_20171227135011530.json"
builder.addBinaryBody("binFile", in, ContentType.DEFAULT_BINARY, filename);// 文件
builder.addTextBody("filename", filename, ContentType.create(ContentType.DEFAULT_TEXT.getMimeType(), "UTF-8"));
post.setEntity(builder.build());

int connTimeout = 10000;
int soTimeout = -1;
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
RequestConfig.Builder configBuilder = RequestConfig.custom();
if (connTimeout > 0) configBuilder.build();
if (soTimeout > 0) configBuilder.setSocketTimeout(soTimeout);
clientBuilder.setDefaultRequestConfig(configBuilder.build());
// 4.3以后会自动在interceptor中实现启用压缩和自动解压,所以不需要gzip的时候需要指定一下---binfile-gzip true/false
if (!false) clientBuilder.disableContentCompression();
//clientBuilder.setProxy(proxy);//----放开抓包
HttpClient client = clientBuilder.build();
HttpResponse httpResponse = client.execute(post);

//返回值封装
String content = new String(EntityUtils.toString(httpResponse.getEntity()).getBytes("ISO-8859-1"),"utf-8");
int statusCode = httpResponse.getStatusLine().getStatusCode();
jqcxResponse.setStatusCode(statusCode);
Header header=httpResponse.getFirstHeader("binfile-md5");
if(header!=null) jqcxResponse.setBinFileMd5(header.getValue());
header=httpResponse.getFirstHeader("Content-Disposition");
if(header!=null) jqcxResponse.setContentDisposition(header.getValue());
header=httpResponse.getFirstHeader("Content-Encoding");
if(header!=null) jqcxResponse.setContentEncoding(header.getValue());
//请求成功获取返回数据
if(statusCode==200){
jqcxResponse.setData(content);
}

代码解析:JqcxResponse是我自己封装起来的model类里面写的是我需要的相关参数,url是主机的地址,也就是我请求的地址。 filecontent是JSONObject转成String的形式内容,转成Inputstream流之后在进行MD5加密。 MultipartEntityBuilder是http传输文件需要的工具类,在代码中我addBinaryBody(就是文件的内容)和addTextBody(就是文件的名称)加进去,把header的相关参数和builder 加入到HttpPost中(也就是和请求地址一起过去的参数) HttpClientBuilder clientBuilder = HttpClientBuilder.create(); HttpClient client = clientBuilder.build(); HttpResponse httpResponse = client.execute(post); 这三个是对请求端建立连接 ,返回HttpResponse里面装载一些需要返回的相关参数。statusCode 这个是请求的状态码 比如200 500 404这里我就不做多解释了。

下面是JqcxResponse的内容:

public class JqcxResponse {
public int getStatusCode() {
return statusCode;
}

public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

private int statusCode;
private String msg;

private String data;

private String binFileMd5;

private String contentDisposition;

private String contentEncoding;

public String getBinFileMd5() {
return binFileMd5;
}

public void setBinFileMd5(String binFileMd5) {
this.binFileMd5 = binFileMd5;
}

public String getContentDisposition() {
return contentDisposition;
}

public void setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
}

public String getContentEncoding() {
return contentEncoding;
}

public void setContentEncoding(String contentEncoding) {
this.contentEncoding = contentEncoding;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}
Last Updated: 1/31/2019, 3:27:13 AM