您的位置:首页 > 编程语言 > Java开发

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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc 浏览器
相关文章推荐