Java 程序式下載

@RequestMapping(value = "/rardown")
public void rardown( HttpServletRequest request, String filename,
HttpServletResponse response)
throws Exception {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

/*
* String ctxPath = request.getSession().getServletContext()
* .getRealPath("/") + FileOperateUtil.UPLOADDIR; String downLoadPath =
* ctxPath + storeName;
*/


String filename1 = request.getParameter("filename");
String docId = request.getParameter("docId");
System.out.println(filename1);
// 獲取目標文件的絕對路徑
String downLoadPath = request.getSession().getServletContext()
.getRealPath("tsresource/" + filename1);
long fileLength = new File(downLoadPath).length();
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(filename1.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();

}
發佈了13 篇原創文章 · 獲贊 8 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章