java HttpClients上传文件
服务端(接受文件)
/**
* 上传文件接口
* @param type
* @return
*/
@RequestMapping(value = "/upFile",method = RequestMethod.POST)
public AjaxResult upFile(@RequestParam("type")int type,@RequestParam("file")MultipartFile file) {
System.out.println(type);
String fileName=file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
String phoName=UUID.randomUUID().toString()+suffix;
/************************************************************************************/
//图片处理开始
if(file.isEmpty()){
return AjaxResult.fail("请选择文件");
}
BufferedImage bi = null;
try {
bi = ImageIO.read(file.getInputStream());
if(bi==null){
return AjaxResult.fail("您上传的不是图片格式");
}
} catch (IOException e) {
e.printStackTrace();
return AjaxResult.fail("您上传的不是图片格式");
}
File path = new File(achievement_dir);//文件保存路径
if(!path.exists()) {
path.mkdirs();
}
File f=new File(path.getAbsolutePath(),phoName);
try {
file.transferTo(f);
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.fail("文件保存异常");
}
//图片处理结束
/********************************************************************************************************/
return AjaxResult.success("success");
}
客户端(发送文件)
package com.dzqc.yx.img;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* 测试
* @author jiajia
*
*/
public class TestMain {
public static void main(String[] args) throws FileNotFoundException {
String res=httpClientUploadFile(new File("D:\\test\\a.png"));
System.out.println(res);
}
/**
* @return 响应结果
*/
public static String httpClientUploadFile(File file) {
final String remote_url = "http://localhost:8095/signup/upFile";// 第三方服务器请求地址
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
try {
String fileName = file.getName();
HttpPost httpPost = new HttpPost(remote_url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file",new FileInputStream(file), ContentType.MULTIPART_FORM_DATA, fileName);//文件流
builder.addTextBody("type","1");//普通字段
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
猜你喜欢
blog
java使用minio上传文件
minio
6
创建buckets
创建serviceaccounts
java上传文件
maven依赖:
dependency
groupIdio.minio/groupId
blog
java使用FTP上传文件
工具
1748
pom依赖dependencygroupIdcommons-net/groupIdartifactIdcommons-net/artifactIdversion3.6/version/dependencyFTP服务类packageclub.jiajiajia.bulider.service;importjava.io.IOException;importjava.io.InputStream;im
blog
java ajax动态上传多个文件
前端(h5)
1651
后端这么写packagecn.com.dzqc.controller;importjavax.servlet.http.HttpServletRequest;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.sp
blog
springboot上传文件与回显
框架
1422
springboot上传文件与回显资源映射路径配置:packagecom.dzqc.yx.controller
blog
springboot上传文件大小限制
框架
2140
$FileSizeLimitExceededException:Thefieldfileexceedsitsmaximumpermittedsizeof1048576bytes.yml文件加配置springboot2.0配置spring:servlet:multipart:max-file-s
框架
5663
一次实现上传文件的进度条原理:在上传文件之前,请求以下服务器,获取一个socketId(作用:用来标识本次连接,服务器端也是通过这个id找到对应的连接,然后给这个连接发送消息(进度)),此时socket
linux系统
914
nginx代理上传文件报413(RequestEntityTooLarge)错误的解决方案RequestEntityTooLarge:请求的实体太大,nginx代理时默认的大小是1M解决方案:设置
java基础
289
核心类ZipOutputStream的api文档请参考:https://nowjava.com/docs/java-api-11/java.base/java/util/zip
最近发表
归档
2018年11月
12
2018年12月
33
2019年01月
28
2019年02月
28
2019年03月
32
2019年04月
27
2019年05月
33
2019年06月
6
2019年07月
12
2019年08月
12
2019年09月
21
2019年10月
8
2019年11月
15
2019年12月
25
2020年01月
9
2020年02月
5
2020年03月
16
2020年04月
4
2020年06月
1
2020年07月
7
2020年08月
13
2020年09月
9
2020年10月
5
2020年12月
3
2021年01月
1
2021年02月
5
2021年03月
7
2021年04月
4
2021年05月
4
2021年06月
1
2021年07月
7
2021年08月
2
2021年09月
8
2021年10月
9
2021年11月
16
2021年12月
14
2022年01月
7
2022年05月
1
2022年08月
1
标签
算法基础
linux
前端
c++
数据结构
框架
数据库
计算机基础
储备知识
java基础
ASM
其他
深入理解java虚拟机
nginx
git
消息中间件
搜索
maven
redis
docker
dubbo
vue
导入导出
软件使用
idea插件
协议
无聊的知识
jenkins
springboot
mqtt协议
keepalived
minio
目录
祝愿神州十三飞行乘组平安归来