java工具类下载_java文件下载工具类
/**
* Desc:下載工具類
* @date 2014-6-26
*/
public class DownLoadUtil {
/**
* Desc:根據文件頭判斷請求來自的瀏覽器,以便有針對性的對文件名轉碼
* @date 2014-6-26
* @param request
* @return
* @throws Exception
*/
public static String transFileName(String fileName, HttpServletRequest request) {
String agent = request.getHeader("USER-AGENT");
//fileName = fileName.trim();//去首尾空格
try {
//根據文件頭判斷請求來自的瀏覽器,以便有針對性的對文件名轉碼
if(null != agent && -1 != agent.indexOf("theworld")){//世界之窗
fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1"); //解決下載的文件名中含有小括號轉變義符%28%29
}else if(null != agent && -1 != agent.indexOf("MSIE 8.0")){//IE8
String lenFileName = URLEncoder.encode(fileName, "UTF-8");
if (lenFileName.length() > 150) {//文件名長度是否大于150個字符
fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
} else {
fileName = URLEncoder.encode(fileName,"UTF-8").replace("+","%20");
}
}else if(null != agent && -1 != agent.indexOf("MSIE 7.0") && -1 != agent.indexOf("SE 2.X MetaSr 1.0")){//sogo瀏覽器
fileName = URLEncoder.encode(fileName,"UTF-8").replace("+","%20");
}else if(null != agent && (-1 != agent.indexOf("SV1") || -1 != agent.indexOf("360SE"))){//360安全瀏覽器
String lenFileName = URLEncoder.encode(fileName, "UTF-8");
if (lenFileName.length() > 150) {//文件名長度是否大于150個字符
fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
} else {
fileName = URLEncoder.encode(fileName,"UTF-8").replace("+","%20");
}
}else if(null != agent && -1 != agent.indexOf("Chrome")){//google
fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1"); //解決下載的文件名中含有小括號轉變義符%28%29
}else if(null != agent && -1 != agent.indexOf("Firefox")){//Firefox
fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
}else if(null != agent && -1 != agent.indexOf("Safari")){//Firefox
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}else {//其它瀏覽器
fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
}
} catch (Exception e) {
throw new RuntimeException("transFileName error", e);
}
return fileName;
}
}
總結
以上是生活随笔為你收集整理的java工具类下载_java文件下载工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: S12卡莎大乱斗出装 天赋怎么选
- 下一篇: beatssolo2和3的区别