JAVA小工具-05-HttpClient/PostMethod上传文件(解决中文文件名乱码问题)

article/2025/10/22 12:54:58

言于头:本节讨论的是在项目中利用HttpClient/PostMethod相关api进行上传文件操作时,会出现上传中文文件名乱码问题。为解决这个问题,下面是总结的一个HTTP工具类以及测试用例。

public class HttpUtils {public static final String UTF_8 = "UTF-8";private static final  Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);private static MultiThreadedHttpConnectionManager connectionManager = null;private static int connectionTimeOut = 60000;private static int socketTimeOut = 60000;private static int maxConnectionPerHost = 20;private static int maxTotalConnections = 20;private static HttpClient client;static {connectionManager = new MultiThreadedHttpConnectionManager();connectionManager.getParams().setConnectionTimeout(connectionTimeOut);connectionManager.getParams().setSoTimeout(socketTimeOut);connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);client = new HttpClient(connectionManager);client = new HttpClient(connectionManager);}/*  ** @description:  上传一个文件* url :请求路径* file: 上传的文件* parmas:其他表单参数**/public static String sendFileRequest(String url, File file,Map<String,String> parmas) {String response = "";PostMethod postMethod = null;// 自定义header:如果需要的话Map headers=new HashMap();try {postMethod = new PostMethod(url){@Overridepublic String getRequestCharSet() {return "UTF-8";}};// 填入header:如果需要的话if (headers != null && headers.size() > 0) {Iterator var5 = headers.entrySet().iterator();while(var5.hasNext()) {Map.Entry entry = (Map.Entry)var5.next();postMethod.setRequestHeader(entry.getKey().toString(), entry.getValue().toString());}}//填入其他表单参数Integer len=parmas.size()+1;Part[] parts=new Part[len];Integer indx=0;for (Map.Entry<String, String> stringStringEntry : parmas.entrySet()) {postMethod.setParameter(stringStringEntry.getKey(),stringStringEntry.getValue());StringPart stringPart=new StringPart(stringStringEntry.getKey(),stringStringEntry.getValue());parts[indx]=stringPart;indx++;}// 自定义文件解析器(解决中文文件乱码),需要注意的是其中 file:对应的是被调用接口中的file 参数CustomFilePart filePart = new CustomFilePart("file", file);parts[parmas.size()]=filePart;postMethod.setRequestEntity(new MultipartRequestEntity(parts,postMethod.getParams()));int statusCode = client.executeMethod(postMethod);if (statusCode == 200) {response = convertStreamToString(postMethod.getResponseBodyAsStream());} else {LOGGER.error("响应状态码 = " + postMethod.getStatusCode()+";响应状态信息 = "+postMethod.getStatusText()+"响应体信息 = "+ postMethod.getResponseBodyAsString());}} catch (HttpException var11) {LOGGER.error("发生致命的异常,可能是协议不对或者返回的内容有问题", var11);} catch (IOException var12) {LOGGER.error("发生网络异常", var12);} finally {if (postMethod != null) {postMethod.releaseConnection();}}return response;}/*** 输入流转字符串* @param inputStream 输入流* @return 返回结果* @throws IOException IO异常*/private static String convertStreamToString(InputStream inputStream) throws IOException {String result = "";ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buffer = new byte[Constant.Numberic.N_1024];int len;// 将输入流转移到内存输入流中try {while ((len = inputStream.read(buffer, Constant.Numberic.N_0, buffer.length)) != Constant.Numberic.NEGATIVE_ONE) {out.write(buffer, Constant.Numberic.N_0, len);}// 将内存流转换成字符串result = new String(out.toByteArray(),"UTF-8");} catch (IOException e) {LOGGER.error("convertStreamToString发生异常",e);} finally {out.close();}return result;}
}

自定义的文件接收器CustomFilePart 继承FilePart :

public class CustomFilePart extends FilePart {public CustomFilePart(String name, File file) throws FileNotFoundException {super(name, file);}@Overrideprotected void sendDispositionHeader(OutputStream out) throws IOException {//super.sendDispositionHeader(out);//下面部分其实为实现父类super.sendDispositionHeader(out) 里面的逻辑out.write(CONTENT_DISPOSITION_BYTES);out.write(QUOTE_BYTES);out.write(EncodingUtil.getBytes(getName(),"utf-8"));out.write(QUOTE_BYTES);String fileName = getSource().getFileName();// 此处为解决中文文件名乱码部分if (!StringUtils.isEmpty(fileName)){out.write(EncodingUtil.getAsciiBytes(FILE_NAME));out.write(QUOTE_BYTES);out.write(EncodingUtil.getBytes(fileName,"utf-8"));out.write(QUOTE_BYTES);}}
}

如下为一个测试用例,过程:
1、先在Controller中编写两个普通的上传文件接口。
2、在其中一个A上传接口中利用上面编写的http调用另外一个B上传接口,并在B接口中打印输出查看效果。
实现如下:

@Controller
@RequestMapping(value = "/test/api/v1")
public class TestController {private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);/*** A上传接口,用于web端调用,此处使用postman测试**/@RequestMapping("/import/file")@ResponseBodypublic Response uploadFile(@RequestParam("file") MultipartFile uploadFile, String objType, String createUser) {try {Map parma=new HashMap();parma.put("objType",objType);parma.put("createUser",createUser);// 此处url为下面的B上传接口String url="http://15.*******:***/test/api/v1/import/file/http";return Response.ok(HttpUtils.sendFileRequest(url,mutilFile2File(uploadFile),parma));}catch (Exception e){LOGGER.error("上传数据文件异常",e);return Response.error("获取uploadFile失败!"+e.getMessage());}}/*** B上传接口,直接打印输出**/@RequestMapping("/import/file/http")@ResponseBodypublic Response uploadFileHttp(@RequestParam("file") MultipartFile uploadFile, Integer objType, String createUser) {try {LOGGER.info("fileName:"+uploadFile.getOriginalFilename()+";objType:"+objType+";createUser:"+createUser);return Response.ok(uploadFile.getOriginalFilename());}catch (Exception e){LOGGER.error("上传数据文件异常uploadFileHttp",e);return Response.error("获取uploadFileHttp失败!"+e.getMessage());}}/*** 将web端上传的MultipartFile 转换成普通的File**/public File mutilFile2File(MultipartFile multipartFile){File file=null;try {File destFile = new File("/upload/data/");if (!destFile.exists()) {destFile.mkdirs();}//将文件存储到本地String fileName=new String(multipartFile.getOriginalFilename().getBytes("utf-8"));file = new File(destFile.getAbsolutePath() + "/" + fileName);multipartFile.transferTo(file);file.createNewFile();}catch (Exception e){LOGGER.error("mutilFile2File异常:",e);}finally {return file;}}
}

postman调用如下:
在这里插入图片描述

B上传接口输出:
在这里插入图片描述


http://chatgpt.dhexx.cn/article/i5AYcSVe.shtml

相关文章

解决PostMethod的中文乱码

解决HttpClient的PostMethod的中文乱码问题 问题场景&#xff1a; 解决代码&#xff1a; 请求时设定编码格式&#xff1a; post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); 完整代码&#xff1a; /*** 封装请求参数&#xff0…

php 取整,PHP取整的方法有哪些

本篇文章主要给大家介绍PHP取整的四种方法。 PHP实现取整的问题&#xff0c;不仅在我们学习PHP过程中会遇到&#xff0c;在我们PHP面试过程中也是常见的考点之一。 下面我们结合简单的示例给大家总结介绍PHP取整的四种方法。 第一种方法&#xff1a;直接取整&#xff0c;舍弃小…

php 如何取整,解析php取整的几种方式

解析php取整的几种方式 floor 舍去法取整 语法格式:float floor ( float value )返回不大于value 的下一个整数&#xff0c;将value 的小数部分舍去取整。floor() 返回的类型仍然是float&#xff0c;因为float 值的范围通常比integer 要大。 echo floor(4.3); // 4 echo floo…

VUE 数据分页

只要涉及到数据查询&#xff0c;通常我们都会进行分页查询。 假设你的表中有上百万条记录&#xff0c;不分页的话&#xff0c;我们不可能一次性将所有数据全部都载入到前端吧&#xff0c;那前后端都早就崩溃了。 结合 Spring Spring 和 Vue 都提供了开箱即用的分页功能。 S…

前端Vue分页及后端PageHelper分页综合运用

分页显示数据对项目开发中尤为重要&#xff0c;同时能提升用户体验&#xff0c;下面的前端css、js是我引用这篇文章的《使用Vue开发一个分页插件》&#xff0c;我在这个的基础上结合了后端稍微完善了一下&#xff0c;修改了disable的样式&#xff0c;在里面加了pointer-events:…

antd design vue分页组件

我们在使用分页组件的时候可以有两种方法&#xff1a; 第一种是直接用表格()的自定义:pagination属性最方便&#xff1b;如下图所示&#xff1a; 第二种是分页组件 这里我总结的是第二种方法的使用&#xff0c;由于是 Ant Design Vue 的组件&#xff0c;所以必须安装Ant Desig…

Vue分页页码栏设计

Vue分页页码栏设计 效果展示HTML数据需要函数需要运用 效果展示 HTML <div class"page_bar no-select"><ul class"clearfix"><li class"iconfont":class"{vh : currentPage 1}"click"subCurrentPage">&…

超级详细:一个漂亮的Vue分页器组件的实现

整篇分两个部分&#xff1a; 思路部分&#xff1a;讲解怎么实现分页器组件【大把时间看-建议】 后面部分&#xff1a;按照步骤&#xff0c;直接引入组件【没有时间看-建议】 思路&#xff1a;基于连续页码进行判断 需要添加分页器的组件&#xff08;Search组件&#xff09;中…

vue实现分页vue分页查询怎么实现

效果图&#xff1a; 代码&#xff1a; 复制过去即可运行 <!DOCTYPE html> <html lang"en" xmlns:th"http://www.w3.org/1999/xhtml"> <head><meta charset"UTF-8"><title>Title</title><!-- <scrip…

vue分页单位设置为中文格式

根据我搭建前端项目时遇到的问题做一个记录&#xff0c;我下载了一个vue-element-admin前端项目demo&#xff0c;但是默认情况下此demo分页展示时为如下图所示&#xff1a; 遇到此种情况想要调整为中文显示时&#xff0c;如下中文显示案例&#xff1a; 此时需要修改demo项目中…

vue分页器的封装

1.需要注册为全局组件 //main.js // 封装分页器为全局组件 Vue.component(Pagination.name,Pagination); 2.父组件的使用分页器&#xff0c;需要传递相应的参数和自定义事件 <Pagination :pageNo"searchParam.pageNo" :pageSize"searchParam.pageSize"…

django与vue分页

后端django进行自定义分页 1.编写自定义配置文件 from rest_framework.pagination import LimitOffsetPaginationclass LimitPagination(LimitOffsetPagination):max_limit 2 # 最大limit限制&#xff0c;默认Nonedefault_limit 2 # 默认限制&#xff0c;和page_size作用…

Django+vue 分页展示

这里提供两种分页方法 一种是手写分页,不常用,但是明白一下分页的逻辑实现 第二种是用heyui提供的组件.很多功能都给封装好了,用起来也比较美观. 手写分页 后端接口 class GoodList(APIView):def get(self, request):# 当前页page int(request.GET.get(page, 1))# 一页有多…

Vue分页及页码跳转

效果如下&#xff1a; HTML&#xff1a; <ul class"page f16 tc mt30"> <li> <span v-if"page > 1"><b click"page--,pageClick()">上一页</b></span> <span v-if"page 1">上一页<…

VUE分页出现省略号

VUE分页出现省略号 废话不多说直接上代码 calcPageNum() {let pageTotal Math.ceil(this.total / this.limit); //获取最大页码数let cur this.currentPage;//获取当前页码数if (pageTotal < 7) {//判断什么时候正常显示return Math.ceil(this.total / this.limit);} els…

vue 分页表格数据导出

vue分页表格数据导出功能&#xff0c;超简单&#xff01;不用安装不用引入 写个方法即可实现 //导出exportExcel() {let url "/VueDemo/api/record/export.htm"; //这里写后台给的接口&#xff0c;注意反向代理也要写上&#xff01;VueDemo是我这边的反向代理url …

vue分页功能Bug

今天分页功能报错了&#xff0c;报错是这样报错的 看代码 <pagination v-show"adminTotal>0":total"adminTotal":page.sync"adminParams.pageNum":limit.sync"adminParams.pageSize"pagination"getAdminListPage" /&…

vue 分页

分页效果实现 思路&#xff1a; 1.每页显示的数量 2.当前页数 3,总页数 , 总页数放在computed中计算 放在页面 splice截取数组元素 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport&qu…

vue分页列表

html部分 css部分 js部分

Vue 怎样实现分页功能

文章目录 数据获取分页器实现页面渲染总结 Vue是一款流行的前端框架&#xff0c;它提供了丰富的API和组件&#xff0c;可以帮助开发者快速地构建现代化的Web应用程序。在Web应用程序中&#xff0c;分页功能是一个非常常见的需求&#xff0c;它可以帮助用户快速地浏览和查找大量…