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

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

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

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.lryc.cn/news/2379829.html

相关文章:

  • STM32实战指南:DHT11温湿度传感器驱动开发与避坑指南
  • 【android bluetooth 协议分析 01】【HCI 层介绍 8】【ReadLocalVersionInformation命令介绍】
  • esp32课设记录(四)摩斯密码的实现 并用mqtt上传
  • 「HHT(希尔伯特黄变换)——ECG信号处理-第十三课」2025年5月19日
  • 前端(vue)学习笔记(CLASS 6):路由进阶
  • GPT-4.1特点?如何使用GPT-4.1模型,GPT-4.1编码和图像理解能力实例展示
  • 使用Python和FastAPI构建网站爬虫:Oncolo医疗文章抓取实战
  • 写一段图片平移的脚本
  • 【C++】哈希的概念与实现
  • Yocto和Buildroot功能和区别
  • 物联网数据湖架构
  • 详解RabbitMQ工作模式之发布订阅模式
  • 什么是子网委派?
  • 微信学习之导航功能
  • 城市内涝监测预警系统守护城市安全
  • 用 CodeBuddy 搭建「MiniGoal 小目标打卡器」:一次流畅的 UniApp 开发体验
  • Web技术与Nginx网站环境部署
  • AI移动监测:仓储环境安全的“全天候守护者”
  • 【数据库】数据库故障排查指南
  • mariadb 升级 (通过yum)
  • 2025年5月华为H12-821新增题库带解析
  • 用 python 编写的一个图片自动分类小程序(三)
  • 用户行为日志分析的常用架构
  • 初识 java
  • 3D 数据交换格式(.3DXML)简介
  • frida 配置
  • 16-看门狗和RTC
  • 【AI论文】用于评估和改进大型语言模型中指令跟踪的多维约束框架
  • AUTOSAR图解==>AUTOSAR_SRS_TimeService
  • 设计模式的原理及深入解析