springmvc文件下载之文件名下划线问题终极解决方案

时间:2022-08-31 13:36:02

直接上代码:Action中代码片段。

@RequestMapping("download")
public String download(ModelMap model, @ModelAttribute("e") Template t, HttpServletResponse response, HttpServletRequest request) throws Exception {
Account acc = getLoginAccount();   
if(acc==null || StringUtils.isBlank(acc.getAccount())){
return ("/account/login");
}
String fileUrl = template.getFileUrl();   //url 路径, 如 http://×××××/×××/××××/image/20170525/中文.jpg
String filename = fileUrl.substring(fileUrl.lastIndexOf("/")+1);  //截取最后的文件名  -> 中文.jpg
filename = processFileName( request, filename);
BufferedOutputStream bf = null;
try {
response.setHeader("Content-disposition", "attachment; filename = " + filename);
bf = new BufferedOutputStream(response.getOutputStream());
bf.write(this.httpConverBytes(fileUrl,request));
}.... 重要的 processFileName方法。
public static String processFileName(HttpServletRequest request, String fileNames) {
String codedfilename = null;
try {
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE") || null != agent
&& -1 != agent.indexOf("Trident")) {// ie
String name = java.net.URLEncoder.encode(fileNames, "UTF8");
codedfilename = name;
} else if (null != agent && -1 != agent.indexOf("Mozilla")) {// 火狐,chrome等
codedfilename = new String(fileNames.getBytes("UTF-8"), "iso-8859-1");
}
} catch (Exception e) {
e.printStackTrace();
}
return codedfilename;
} //httpConverBytes 方法
public static byte[] httpConverBytes(String path,HttpServletRequest request) {
BufferedInputStream in = null;
ByteArrayOutputStream out = null;
URLConnection conn = null;
int httpResult=0;
try {
StringBuffer sb = new StringBuffer();
for(int i=0;i<path.length();i++){
char a=path.charAt(i);   //url路径的中文部分重新编码 很重要
if(a>127){//将中文UTF-8编码
sb.append(URLEncoder.encode(String.valueOf(a), "utf-8"));
}else{
sb.append(String.valueOf(a));
}
}
URL url = new URL(sb.toString()); //创建URL
URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码urlconn.connect();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
httpResult = httpconn.getResponseCode();
in = new BufferedInputStream(httpconn.getInputStream()); if (httpResult != HttpURLConnection.HTTP_OK){ //不等于HTTP_OK说明连接不成功
System.out.print("连接失败!");
}else {
out = new ByteArrayOutputStream(1024);
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
byte[] content = out.toByteArray();
return content;
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} 通过以上处理下划线问题解决了。
springmvc文件下载之文件名下划线问题终极解决方案
 

springmvc文件下载之文件名下划线问题终极解决方案的更多相关文章

  1. 前端axios请求二进制数据流转换生成PDF文件空白问题(终极解决方案)

    本文章共1570字,预计阅读时间1 - 3分钟. 问题场景: axios请求二进制数据转换生成PDF空白问题,使用axios请求后端接口,后端返回的二进制流文件,需要转换成PDF,但是在postman ...

  2. 使用SpringMVC框架实现文件上传和下载功能

    使用SpringMVC框架实现文件上传和下载功能 (一)单个文件上传 ①配置文件上传解释器 <!—配置文件上传解释器 --> <mvc:annotation-driven>&l ...

  3. SpringMVC文件下载与JSON格式

    点击查看上一章 现在JSON这种数据格式是被使用的非常的广泛的,SpringMVC作为目前最受欢迎的框架,它对JSON这种数据格式提供了非常友好的支持,可以说是简单到爆. 在我们SpringMVC中只 ...

  4. 关于springmvc下服务器文件打包成zip格式下载功能

    关于springmvc下服务器文件打包成zip格式下载功能 2016年09月21日 11:22:14 toxic_guantou 阅读数:5731更多 个人分类: 技术点存储   版权声明:本文为博主 ...

  5. SpringMVC&plus;Ajax实现文件批量上传和下载功能实例代码

    需求: 文件批量上传,支持断点续传. 文件批量下载,支持断点续传. 使用JS能够实现批量下载,能够提供接口从指定url中下载文件并保存在本地指定路径中. 服务器不需要打包. 支持大文件断点下载.比如下 ...

  6. springmvc上传文件,抄别人的

    SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是 ...

  7. Eclipse不自动编译java文件的终极解决方案

    最近我的eclipse经常犯傻,项目中总是有很多,启动项目也是没有启动类.查了下项目中生成的class文件,我靠竟然没有,或者还是以前的.原来是eclipse犯傻了,它没帮我自动编译java文件.一般 ...

  8. 深入springMVC源码------文件上传源码解析&lpar;下篇&rpar;

    在上篇<深入springMVC------文件上传源码解析(上篇) >中,介绍了springmvc文件上传相关.那么本篇呢,将进一步介绍springmvc 上传文件的效率问题. 相信大部分 ...

  9. SpringMVC上传文件

    SpringMVC中上传文件还是比较方便的,Spring内置了一些上传文件的支持类,不需要复杂的操作即可上传文件. 文件上传需要两个jar支持,一个是commons-fileupload.jar和co ...

随机推荐

  1. Python2&period;7&lt&semi;--------&gt&semi;Python3&period;x

    版本差异 from __future__   Python2.7 Python3.x 除法 / // Unicode u''                                       ...

  2. 移动web在ios和android下点击元素出现阴影问题

     移动web开发经验总结 1.-webkit-tap-highlight-color:rgba(255,255,255,0)可以同时屏蔽ios和android下点击元素时出现的阴影.备注:transp ...

  3. 使用C&num;通过调用minitab的COM库自动化生成报表

    本文介绍通过C#调用minitab com组建自动化生成报表的方法. 首先需要在minitab中通过手动配置的方式生成报表来得到该报表的命令行,过程如下 选择菜单“编辑器”->“启用命令”启用命 ...

  4. iOS----------上传遇到的问题

    插叙:之前电脑一直遇到VPN登录不了的问题,试了几台电脑都能正常连接,只有我的电脑不可以,VPN大佬建议我直接重装系统,索性就直接重新装了系统,结果就能连接了.昨天开始上传包的时候,发现用Applic ...

  5. Centos7 下yum安装mysql

  6. &lowbar;ZNote&lowbar;Window&lowbar;技巧&lowbar;删除开机启动项

    win + R 输入msconfig 可以打开

  7. Golang 方法method

    方法method Go中虽没有class,但依旧有method 通过显示说明receiver来实现与某个类型的结合 只能为同一个包中的类型定义方法 receiver可以是类型的值或者指针 不存在方法重 ...

  8. Cannot sending data from mongodb into elasticsearch using logstash

    Question: Hi all, i have an issue when i try to get data from mongodb to elasticsearch using logstas ...

  9. &lbrack;BZOJ4517&rsqb;&lbrack;SDOI2016&rsqb;排列计数&lpar;错位排列&rpar;

    4517: [Sdoi2016]排列计数 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 1616  Solved: 985[Submit][Statu ...

  10. IOS面试题(二)

    50. 谈谈对Block 的理解?并写出一个使用Block执行UIVew动画? 答:Block是可以获取其他函数局部变量的匿名函数,其不但方便开发,并且可以大幅提高应用的执行效率(多核心CPU可直接处 ...