当前位置: 首页 > java >正文

下载和导出文件名称乱码问题

只对文件名称进行乱码处理,和文件中的内容无关。

import lombok.SneakyThrows;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;/*** 下载相关工具类* @author yzx*/
public class FileCodeUtil {public static void writeHeader(String fileName){HttpServletRequest req = getRequest();HttpServletResponse rep = getResponse();rep.setContentType("application/octet-stream");String agent = req.getHeader("USER-AGENT").toLowerCase();try{rep.setHeader("Content-Disposition" , "attachment;" + encodeFileName(agent , fileName));}catch(Exception e){e.printStackTrace();}rep.setCharacterEncoding("UTF-8");}public static String encodeFileName(String fileName)throws UnsupportedEncodingException {String agent = getRequest().getHeader("USER-AGENT").toLowerCase();return encodeFileName(agent, fileName);}@SneakyThrowsprivate static String encodeFileName(String userAgent, String fileName) {String newFileName = URLEncoder.encode(fileName, "UTF-8");String rtn = (new StringBuilder()).append("filename=").append(newFileName).append("").toString();if (userAgent != null) {userAgent = userAgent.toLowerCase();if(userAgent.indexOf("msie") != -1)rtn = (new StringBuilder()).append("filename=").append(newFileName).append("").toString();else if(userAgent.indexOf("opera") != -1)rtn = (new StringBuilder()).append("filename*=UTF-8''").append(newFileName).toString();else if(userAgent.indexOf("safari") != -1)rtn = (new StringBuilder()).append("filename=").append(new String(newFileName.getBytes("UTF-8"), "ISO8859-1")).append("").toString();else if(userAgent.indexOf("applewebkit") != -1)rtn = (new StringBuilder()).append("filename=").append(newFileName).append("").toString();else if(userAgent.indexOf("mozilla") != -1)rtn = (new StringBuilder()).append("filename*=UTF-8''").append(newFileName).toString();}return rtn;}/*** 获取当前请求的HttpServletRequest对象** @return HttpServletRequest* @throws IllegalStateException 如果当前没有请求上下文*/public static HttpServletRequest getRequest() {RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();if (requestAttributes == null) {throw new IllegalStateException("No current request context available");}return ((ServletRequestAttributes) requestAttributes).getRequest();}/*** 获取当前请求的HttpServletResponse对象** @return HttpServletResponse* @throws IllegalStateException 如果当前没有请求上下文*/public static HttpServletResponse getResponse() {RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();if (requestAttributes == null) {throw new IllegalStateException("No current request context available");}return ((ServletRequestAttributes) requestAttributes).getResponse();}}

经常用,总结一点点。。。。。。。。。。。

http://www.xdnf.cn/news/7276.html

相关文章:

  • SVG 与 Canvas 技术调研对比
  • BeanFactory和FactoryBean的区别
  • 【Vue篇】潮汐中的生命周期观测站​
  • 10.14 Function Calling与Tool Calling终极解析:大模型工具调用的5大核心差异与实战优化
  • 基于python版本secsgem源码开发gem,应该如何设置devicetype、connectmode
  • Python训练营打卡 Day30
  • Docker安装MySQL数据库-单机版
  • 不锈钢保温容器行业2025数据分析报告
  • Hi3516CV610车牌识别算法源码之——实时从sensor采集视频 识别车牌
  • 使用PHP对接东南亚、日本、印度和印度尼西亚股票数据源
  • 医疗行业手持终端:推动医疗智能化的关键力量
  • 网络Tips20-007
  • lammps后处理:堆垛层错和孪晶的数量统计
  • 养生指南:解锁健康生活新方式
  • 阿里云web端直播(前端部分)
  • kotlin flow的两种SharingStarted策略的区别
  • 远程医疗结合贴肤芯片技术对体育院校学生提升运动表现的路径分析
  • 五分钟完成PolarDB替换postgresql
  • codeup添加流水线docker自动化部署
  • 初识MySQL · 索引
  • Text2SQL在Spark NLP中的实现与应用:将自然语言问题转换为SQL查询的技术解析
  • spring中的EnvironmentPostProcessor接口详解
  • 小乌龟git中的推送账户、作者账户信息修改
  • C#:多线程
  • 关于百度地图JSAPI自定义标注的图标显示不完整的问题(其实只是因为图片尺寸问题)
  • 2025.5.19总结
  • 使用 Qt QGraphicsView/QGraphicsScene 绘制色轮
  • k8s集成环境中pod运行的容器退出码141故障解决方案及排查方向,其他退出码也可以参考此篇
  • Linux内核深入学习(4)——内核常见的数据结构2——红黑树
  • 多模态大语言模型arxiv论文略读(八十三)