HttpClient https上传文件与form表单
2016-10-19 09:51
411 查看
//上传文件
//上传form表单
public String download(String urlStr, String filePath, String uploadTime,
String batchFileName, String downloadPassword){
FileOutputStream fout = null;
InputStream in = null;
try{
HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Protocol authhttps = new Protocol("https",
new AuthSSLProtocolSocketFactory(new URL((new StringBuilder(
"file:")).append(SSLEntity.filePath).toString()),
SSLEntity.password, new URL(
(new StringBuilder("file:")).append(
SSLEntity.filePath).toString()),
SSLEntity.password), 8441);
Protocol.registerProtocol("https", authhttps);
client.getParams().setSoTimeout(60000);
Part[] parts = { new StringPart("uploadTime", uploadTime),
new StringPart("batchFileName", batchFileName),
new StringPart("downloadPassword", downloadPassword)};
PostMethod postMethod = new PostMethod(urlStr);
postMethod.setRequestEntity(new MultipartRequestEntity(parts,
postMethod.getParams()));
if (client.executeMethod(postMethod) == 200) {
Header[] headers = postMethod
.getResponseHeaders("Content-disposition");
String headersFileName = headers[0].getValue();
String strInfo[] = headersFileName.split("\\|");
String suffixs[] = headersFileName.replaceAll("\"", "")
.split("\\.");
if (suffixs.length < 2) {
String jsonInfo = strInfo[0].split("\\=")[1];
return URLDecoder.decode(jsonInfo, "UTF-8");
}
try{
File path = new File(filePath);
if (!path.exists())
path.mkdirs();
batchFileName = (new StringBuilder(String.valueOf(filePath)))
.append(filePath.endsWith(File.separator) ? ""
: File.separator)
.append(strInfo[1].split("\\=")[1]).toString();
in = postMethod.getResponseBodyAsStream();
File file = new File(batchFileName);
fout = new FileOutputStream(file);
int ret = -1;
byte tmp[] = new byte[1024];
while ((ret = in.read(tmp)) != -1)
fout.write(tmp, 0, ret);
fout.flush();
}catch(Exception e){
e.printStackTrace();
return Result.toFailedJson(e.getMessage());
}finally{
fout.close();
in.close();
}
}
}catch(Exception e){
e.printStackTrace();
return Result.toFailedJson(e.getMessage());
}
return Result.toSuccessJson("批量文件下载成功");
}
注:该方式jdk6可能会报握手失败,可切成jdk7或者使用另一篇文章中CloseableHttpClient方式进行通讯。如果有大神知道上述方式怎么设置https的协议版本(TLSv1),望留言告之,谢谢
public String send(String urlStr, File file, String downFilePwd){ InputStream responseStream = null; String responseBody = ""; try{ HttpClient client = new HttpClient(); client.getParams().setAuthenticationPreemptive(true); Protocol authhttps = new Protocol("https", new AuthSSLProtocolSocketFactory(new URL((new StringBuilder( "file:")).append(SSLEntity.filePath).toString()), SSLEntity.password, new URL( (new StringBuilder("file:")).append( SSLEntity.filePath).toString()), SSLEntity.password), 8441); Protocol.registerProtocol("https", authhttps); client.getParams().setSoTimeout(60000); PostMethod postMethod = new PostMethod(urlStr); Part[] parts = { new FilePart("file", file), new StringPart("downloadPassword", downFilePwd) }; postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); if (client.executeMethod(postMethod) == 200) { responseStream = postMethod.getResponseBodyAsStream(); if (responseStream != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] receiveBuffer = new byte[2048]; int readBytesSize = responseStream.read(receiveBuffer); while (readBytesSize != -1) { bos.write(receiveBuffer, 0, readBytesSize); readBytesSize = responseStream.read(receiveBuffer); } responseBody = new String(bos.toByteArray(), "UTF-8"); } } }catch(Exception e){ e.printStackTrace(); return Result.toFailedJson(e.getMessage()); }finally{ try { responseStream.close(); } catch (IOException e) { e.printStackTrace(); } } return responseBody; }
//上传form表单
public String download(String urlStr, String filePath, String uploadTime,
String batchFileName, String downloadPassword){
FileOutputStream fout = null;
InputStream in = null;
try{
HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Protocol authhttps = new Protocol("https",
new AuthSSLProtocolSocketFactory(new URL((new StringBuilder(
"file:")).append(SSLEntity.filePath).toString()),
SSLEntity.password, new URL(
(new StringBuilder("file:")).append(
SSLEntity.filePath).toString()),
SSLEntity.password), 8441);
Protocol.registerProtocol("https", authhttps);
client.getParams().setSoTimeout(60000);
Part[] parts = { new StringPart("uploadTime", uploadTime),
new StringPart("batchFileName", batchFileName),
new StringPart("downloadPassword", downloadPassword)};
PostMethod postMethod = new PostMethod(urlStr);
postMethod.setRequestEntity(new MultipartRequestEntity(parts,
postMethod.getParams()));
if (client.executeMethod(postMethod) == 200) {
Header[] headers = postMethod
.getResponseHeaders("Content-disposition");
String headersFileName = headers[0].getValue();
String strInfo[] = headersFileName.split("\\|");
String suffixs[] = headersFileName.replaceAll("\"", "")
.split("\\.");
if (suffixs.length < 2) {
String jsonInfo = strInfo[0].split("\\=")[1];
return URLDecoder.decode(jsonInfo, "UTF-8");
}
try{
File path = new File(filePath);
if (!path.exists())
path.mkdirs();
batchFileName = (new StringBuilder(String.valueOf(filePath)))
.append(filePath.endsWith(File.separator) ? ""
: File.separator)
.append(strInfo[1].split("\\=")[1]).toString();
in = postMethod.getResponseBodyAsStream();
File file = new File(batchFileName);
fout = new FileOutputStream(file);
int ret = -1;
byte tmp[] = new byte[1024];
while ((ret = in.read(tmp)) != -1)
fout.write(tmp, 0, ret);
fout.flush();
}catch(Exception e){
e.printStackTrace();
return Result.toFailedJson(e.getMessage());
}finally{
fout.close();
in.close();
}
}
}catch(Exception e){
e.printStackTrace();
return Result.toFailedJson(e.getMessage());
}
return Result.toSuccessJson("批量文件下载成功");
}
注:该方式jdk6可能会报握手失败,可切成jdk7或者使用另一篇文章中CloseableHttpClient方式进行通讯。如果有大神知道上述方式怎么设置https的协议版本(TLSv1),望留言告之,谢谢
相关文章推荐
- CloseableHttpClient https上传文件与form表单
- httpClient 使用multipart/form-data 类型上传文件及表单
- HttpClient 4.3.6 使用MultipartEntityBuilder实现类似form表单提交方式的文件上传
- android向服务器上传multipart/form-data文件(upload using multipart post using httpclient in android)
- 使用httpClient MultipartRequestEntity文件上传解析文件和普通表单参数
- 使用Javascript XMLHttpRequest模拟表单(Form)提交上传文件
- Multipart/form-data POST文件上传详解 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form me
- ajax上传文件,提交表单,使用FormData和XMLHttpRequest提交表单
- HttpClient4.5.2模拟表单post提交上传文件
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- HTTPCLIENT POST 模拟有上传文件和文本字段的动态表单
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- httpclient4.3.X post请求上传,将文件封装到请求体中(不以form表单形式)
- nodejs-http 对form表单上传文件数据的解析过程
- 使用ajax提交form表单,包括ajax文件上传 转http://www.cnblogs.com/zhuxiaojie/p/4783939.html
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- ajax上传文件,提交表单,使用FormData和XMLHttpRequest提交表单
- WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)
- HttpClient(五)-- 模拟表单上传文件
- httpurlconnection模拟post提交form表单(普通文本和上传文件) (