java文件下载至linux乱码,JAVA 文件下载乱码问题解决办法
頁面設置隱藏的iframe
頁面下載按鈕
id="btnDown" href="javascript:void(download('btnDown'))" >下載模板
頁面JS腳本
function download(id){
$("#reqFrame").attr("src",encodeURI("/servlet/Common?action=downloadByPath&filePath="+$("#"+id).attr("title")));
}
后臺servlet方法
public class SV_Common extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final Logger logger = Logger.getLogger(SV_Common.class
.getName());
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
try {
if ("downloadByPath".equals(action)) { //下載指定路徑的文件
downloadByPath(request, response);
} else {
throw new IllegalArgumentException("沒有相匹配的操作類型,請檢查opp變量.");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void downloadByPath(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String filePath = request.getParameter("filePath");
logger.info("filePath="+filePath);
String fileFullName = filePath.substring(filePath.lastIndexOf("/") + 1);
response.reset();
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
logger.info("User-Agent=firefox");
response.setHeader(
"Content-Disposition",
"attachment;filename=" + new String(fileFullName.getBytes("UTF-8"), "ISO8859-1")
);
} else {
logger.info("User-Agent=not firefox");
response.setHeader(
"Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileFullName, "UTF-8")
);
}
File file = new File(PathUtil.getPath("") + "/" + filePath);
response.setContentType(new MimetypesFileTypeMap().getContentType(file));
OutputStream out = response.getOutputStream();
BufferedInputStream in = null;
byte[] buffer = new byte[8192];
int length;
try
{
in = new BufferedInputStream(
new FileInputStream(file),8192
);
while ( (length = in.read(buffer)) != -1)
{
out.write(buffer, 0 ,length);
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (out != null)
{
try {out.close();}
catch (IOException ex) {}
out = null;
}
if (in != null)
{
try {in.close();}
catch (IOException ex) {}
in = null;
}
}
response.flushBuffer();
};
}
paip.java swt 亂碼問題解決
paip.java swt 亂碼問題解決 看累掛,Dfile.encoding是gbk的.. 作者Attilax ?艾龍, ?EMAIL:1466519819@qq.com? 來源:attilax的專 ...
ASP+Access UTF-8 網頁亂碼問題解決辦法
用ACCESS數據庫和ASP做網站時用UTF-8編碼有時會出現亂碼,再者網頁出錯或者刷新頁面后就是亂碼,如果數據庫取值亂碼在開頭加上
openerp 7.0郵件接收中文附件亂碼問題解決辦法
openerp 7.0郵件接收中文附件亂碼問題解決辦法: 修改文件\addons\mail\mail_thread.py #1064 line插入代碼: h=email.Header.Header(n ...
關于jFinal開發中遇到的中文亂碼問題解決辦法
關于jFinal開發中遇到的中文亂碼問題解決辦法 設置tomcat的編碼,修改
node-canvas
1.使用之前需要預先安裝??Cairo 本人安裝遇到各種各樣的坑,可以參考這里來填坑:https://github.com/Automattic/node-canvas/wiki/Installati ...
【轉】 SED多行模式空間
1. sed執行模板=sed?'模式{命令1;命令2}' 即逐行讀入模式空間,執行命令,最后輸出打印出來 2. 為方便下面,先說下p和P,p打印當前模式空間內容,追加到默認輸出之后,P打印當前模式空間 ...
LSM
1.MySQL存儲引擎: B+樹 https://blog.csdn.net/qq_26222859/article/details/80631121 2.HBase LSM樹 核心:將對數據的修改增 ...
[原]關于helios自定義面板簡述
想研究一下helios和自己仿真軟件的適配,于是找了一下關于helios的使用 首先關于helios的使用有一個“vr2009”的發帖者有過很詳細描述: http://www.insky.cn/bbs ...
HTML Tags
While some tags have a very specific purpose, such as image and video tags, most tags are used to de ...
重整ADO.NET連接池相關資料
https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.connectionstring(VS.80) ...
ural Ambitious Experiment 樹狀數組
During several decades, scientists from planet Nibiru are working to create an engine that would all ...
總結
以上是生活随笔為你收集整理的java文件下载至linux乱码,JAVA 文件下载乱码问题解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: myeclipse导入项目报错Targe
- 下一篇: 逆水寒最新的服务器7月12日,《逆水寒》