SpringMvc下载文件,判断不同浏览器的中文名字显示
2016-12-27 16:05
381 查看
package com.app.controller.common; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Date; import java.util.StringTokenizer; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.aspectj.util.FileUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.app.aop.LoginCheck; import com.app.aop.SystemControllerLog; import com.app.util.BaseController; import com.app.util.StringUtil; @Controller @RequestMapping(value="/fileOperate") public class FileOperateController extends BaseController{ @RequestMapping(value = "/downLoadFile") @LoginCheck(description = true) @SystemControllerLog(description = "文件下载",modelName="基础模块") @ResponseBody public void downLoadFile(String path, String zsName,HttpServletRequest request, HttpServletResponse response) { try { String agent = request.getHeader("User-Agent").toLowerCase(); System.out.println("浏览器版本:"+getBrowserName(agent,zsName)); //根据不同的浏览器 进行文件名编译 zsName = getBrowserName(agent,zsName); //如果文件真实名字没传 系统给生成一个当前时间的毫秒值+path路径的文件后缀 if (zsName == null || "".equals(zsName.trim())) { int indexOf = path.lastIndexOf("."); String fjType = path.substring(indexOf, path.length()); zsName = new Date().getTime() + fjType; }else{ } path = URLDecoder.decode(path, "UTF-8"); //文件下载根目录 正式环境下 需要换成linux下的路径 File file = new File(path); OutputStream out = null; FileInputStream in = null; if (file != null && file.exists()) { response.setContentType("application/x-download"); try { in = new FileInputStream(path); out = response.getOutputStream(); response.addHeader("Content-Disposition", "attachment;filename="+zsName); FileUtil.copyStream(in, out); } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } finally { in = null; } try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } finally { out = null; } } }else{ response_write("失败",response); } } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } } public String getBrowserName(String agent,String zsName) { try { if(agent.indexOf("msie 7")>0){ return zsName; }else if(agent.indexOf("msie 8")>0){ return zsName; }else if(agent.indexOf("msie 9")>0){ return zsName; }else if(agent.indexOf("msie 10")>0){ return zsName; }else if(agent.indexOf("msie")>0){ return zsName; }else if(agent.indexOf("opera")>0){ return zsName; }else if(agent.indexOf("opera")>0){ return zsName; }else if(agent.indexOf("firefox")>0){//火狐浏览器 //System.err.println("firefox"); return zsName; }else if(agent.indexOf("webkit")>0){//谷歌浏览器 //System.err.println("webkit"); return zsName; }else if(agent.indexOf("gecko")>0 && agent.indexOf("rv:11")>0){//IE11浏览器 //System.err.println("IE11"); zsName = StringUtil.strToUTF8(zsName); zsName = URLEncoder.encode(zsName,"UTF-8"); if(zsName.length()>150){//解决IE 6.0 bug zsName=new String(zsName.getBytes("GBK"),"ISO-8859-1"); } return zsName; }else{ //return "Others"; return zsName; } } catch (Exception e) { e.printStackTrace(); } return null; } }
相关文章推荐
- 不同浏览器下载文件 中文名字乱码解决
- web开发时在浏览器上下载文件中文名字乱码问题
- Java strtus2文件下载不同浏览器文件名中文乱码处理
- struts2 下载文件不能显示中文名字的问题
- 下载文件时兼容不同浏览器中文乱码解决
- PHP解决浏览器 下载文件 中文名称乱码问题--兼容大多浏览器
- 下载文件出现提示框或者直接显示在浏览器中
- jspsmart 下载文件不支持中文 升级使其另存为时显示中文,但打开时还是乱码(已修改正常)
- 文件下载及文件名为中文的时候各个浏览器出现乱码的解决方案
- JAVA 文件上传后台判断浏览器的类型以及JDK1.5和1.6处理的不同
- java下载文件在各个浏览器差异 以及中文乱码解决
- 不同浏览器上中文文件名的下载乱码问题
- moodle中上传和显示中文名字文件
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)
- 在Servlet中以下载的方式向浏览器写数据--中文文件乱码问题解决
- C# 创建xml文档. xml文件中有中文字符,无法用浏览器显示
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)
- 下载文件出现提示框或者直接显示在浏览器中_asp.net技巧
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)