利用接口发送文件
2016-07-12 17:59
295 查看
//向平台发送图片
public static void main(String[] args) throws Exception{
String response="";
PostMethod postMethod = new PostMethod("http://172.16.2.79:8080/zdxqcg/api/workflow/file");
HttpClient client = new HttpClient();
String fileuuid = UuidUtil.get32UUID();
try {
FilePart fp = new FilePart("file",new File("C:/tomcat/apache-tomcat-7.0.54/webapps/ROOT/uploadFiles/uploadImgs/20160712/1468296128108.jpg"));
fp.setCharSet("utf-8");
fp.setContentType("image/*"); //图片格式
//StringPart:普通文本参数
StringPart idPart = new StringPart("fileuuid",fileuuid);
Part[] parts = {idPart,fp};
//HttpClient使用MultipartRequestEntity包装
MultipartRequestEntity mre = new MultipartRequestEntity(parts,postMethod.getParams());
postMethod.setRequestEntity(mre);
//由于要上传的文件可能比较大,因此在此设置最大的连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(postMethod);
System.out.println(status);
System.out.println("提交成功");
if(status ==HttpStatus.SC_OK){
InputStream inputStream = postMethod.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer =new StringBuffer();
String str ="";
while ((str = br.readLine())!=null){
stringBuffer.append(str);
}
response = stringBuffer.toString();
}else{
response = "fail";
}
System.out.println(response);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
String response="";
PostMethod postMethod = new PostMethod("http://172.16.2.79:8080/zdxqcg/api/workflow/file");
HttpClient client = new HttpClient();
String fileuuid = UuidUtil.get32UUID();
try {
FilePart fp = new FilePart("file",new File("C:/tomcat/apache-tomcat-7.0.54/webapps/ROOT/uploadFiles/uploadImgs/20160712/1468296128108.jpg"));
fp.setCharSet("utf-8");
fp.setContentType("image/*"); //图片格式
//StringPart:普通文本参数
StringPart idPart = new StringPart("fileuuid",fileuuid);
Part[] parts = {idPart,fp};
//HttpClient使用MultipartRequestEntity包装
MultipartRequestEntity mre = new MultipartRequestEntity(parts,postMethod.getParams());
postMethod.setRequestEntity(mre);
//由于要上传的文件可能比较大,因此在此设置最大的连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(postMethod);
System.out.println(status);
System.out.println("提交成功");
if(status ==HttpStatus.SC_OK){
InputStream inputStream = postMethod.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer =new StringBuffer();
String str ="";
while ((str = br.readLine())!=null){
stringBuffer.append(str);
}
response = stringBuffer.toString();
}else{
response = "fail";
}
System.out.println(response);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
相关文章推荐
- dancing link 精确覆盖 重复覆盖 (DLX)
- javascript中关于日期和时间的基础知识
- jquery设置select选中的文本
- hibernate4与hibernate5的一些区别
- Android Camera API2.0下全新的Camera FW/HAL架构简述
- hdu 2124 Repair the Wall
- Python对象简介
- iOS开发之功能模块--推送之坑问题解决
- httpclient 在获取返回值时,使用getResponseBodyAsString触发警告的问题
- apache如何在一个IP地址配置多个域名
- tomcat7 优化前后对比
- 如何在依赖aar的情况下进行Java单元测试
- Python 数据类型
- gulp该用起来了,简单的demo入门
- NYOJ-管闲事的小明
- shiro与spring整合
- OpenBTS+USRP安装简明流程(amd64,Ubuntu 14.04.4 LTS)
- 根据cookbook, 多线程情况的事件驱动IO
- boost(一)
- Practical Rendering and Computation with Direct3D 11学习笔记(2.2.2)