Httpclient文件上传
public static void upload(String url,File file,String filename) {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
httppost.setConfig(requestConfig);
FileBody bin = new FileBody(file);
StringBody comment = new StringBody(filename, ContentType.TEXT_PLAIN);
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("filename", comment).build();
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseEntityStr = EntityUtils.toString(response.getEntity());
System.out.println(responseEntityStr);
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@PostMapping("/upload")
public Result upload(@RequestParam("file") MultipartFile file, String filename){
Result s = new Result(1, "success");
String usrHome = System.getProperty("user.home");
try {
String path = usrHome+"/image/";
path = path.replace("\\","/");
System.out.println(path);
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
UploadUtils.uploadFileTest(file,path,filename);
}catch (Exception e){
s.setCode(0);
s.setMessage("失败");
}
return s ;
}
public static void uploadFileTest(MultipartFile zipFile,String targetFilePath,String fileName) {
File targetFile = new File(targetFilePath + File.separator + fileName);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(targetFile);
IOUtils.copy(zipFile.getInputStream(), fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
}
}
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
Httpclient文件上传的更多相关文章
- HttpClient文件上传下载
1 HTTP HTTP 协议可能是如今 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序须要直接通过 HTTP 协议来訪问网络资源. 尽管在 JDK 的 java.net ...
- springMVC + hadoop + httpclient 文件上传请求直接写入hdfs
1.首先是一个基于httpclient的java 应用程序,代码在这篇文章的开头:点击打开链接 2.我们首先写一个基于springMVC框架的简单接收请求上传的文件保存本地文件系统的demo,程序代码 ...
- Android开发之httpclient文件上传实现
文件上传可能是一个比較耗时的操作,假设为上传操作带上进度提示则能够更好的提高用户体验,最后效果例如以下图: 项目源代码:http://download.csdn.net/detail/shinay/4 ...
- httpclient 文件上传
/** * 上传文件 */ public static Boolean uploadFile(String fileName, String url) { ...
- HttpClient多文件上传代码及普通参数中文乱码问题解决
该随笔记录了在实际项目中使用HttpClient调用外部api,需上传文件和普通参数的代码. 笔者在使用 HttpClient 调用 http api 接口时,需要服务端上传文件和一些普通参数给 ht ...
- HttpClient中转上传文件
场景:客户端(浏览器)A---->选择文件上传---->服务器B---->中转文件---->服务器C---->返回结果---->服务器B---->客户端A 有 ...
- 转 Android网络编程之使用HttpClient批量上传文件 MultipartEntityBuilder
请尊重他人的劳动成果,转载请注明出处:Android网络编程之使用HttpClient批量上传文件 http://www.tuicool.com/articles/Y7reYb 我曾在<Andr ...
- 转 使用 HttpClient 4 进行文件上传
http://www.tuicool.com/articles/Y7reYb 1. 概述 本教程我们将描述如何使用 HttpClient 4进行一次多文件上传操作 . 我们将使用 http://ec ...
- HttpClient构造文件上传
在项目中我们有时候需要使用到其他第三方的api,而有些api要求我们上传文件,search一下,下面将结果记录一下喽! 含义 ENCTYPE="multipart/form-data&quo ...
随机推荐
- Hello 2019 D 素因子贡献法计算期望 + 概率dp + 滚动数组
https://codeforces.com/contest/1097/problem/D 题意 给你一个n和k,问n经过k次操作之后留下的n的期望,每次操作n随机变成一个n的因数 题解 概率dp计算 ...
- Ubuntu 获取内核源码树
输入:apt-cache search linux-source //查看内核版本 输入:apt-get install linux-source-3.0.0 //获取对应版本的内核,默认安装在/us ...
- lower_case_table_names与表格名称大小写的问题
1 简介 在MySQL中,数据库对应数据目录中的目录.数据库中的每个表至少对应数据库目录中的一个文件(也可能是多个,取决于存储引擎).因此,所使用操作系统的大小写敏感性决定了数据库名和表名的大小写敏感 ...
- netcore与ef资料收集
http://www.cnblogs.com/cgzl/p/7661805.html https://www.cnblogs.com/cgzl/p/7675485.html https://www.c ...
- Django文件上传【单个/多个图片上传】
准备工作 python:3.6.8 django:2.2.1 新建django项目 确定项目名称.使用的虚拟环境[当然这个也可以后期修改].app的名称 创建成功,选择在新的窗口中打开 图片上传 修改 ...
- VMWare 下安装 Windows XP
准备工作 安装VMware 我这里安装的是 VMware-workstation-full-12.1.1-3770994.exe 下载Windows XP with SP3 从网上下载的MSDN版本X ...
- 【01】Jenkins:安装配置
写在前面的话 从我的工作经历来看,刚出来的时候的第一家公司我们上线采用的是脚本上线.就是那种开发合并完代码以后,在一个固定的时间点,我们上服务器执行更新脚本打包更新.这种方法有一个很大的问题,就是对于 ...
- [转] vue 自定义组件使用v-model
<input v-model="something"> v-model指令其实是下面的语法糖包装而成: <input :value="something ...
- asp.net 版本一键升级,后台直接调用升级脚本
应客户需求,要求实现一个版本一键升级的功能,咨询过同事之后弄了个demo出来,后台代码如下: //DBConnModelInfo:连接字符串的对象 (包含数据库实例名,数据库名,登陆名,登陆密码) p ...
- CLOS : Common Lisp 的面向对象支持
1. defclass ( :accessor/reader/writer ; :initarg ; :initform 2. defgeneric 3. defmethod ----- ...