以流的方式下载附件
程序员文章站
2024-03-07 12:52:03
...
<a onclick="downloadFile(filePath)" href="#">(下载)</a> <iframe name="hidden_frame" id="hidden_frame" style="display:none"></iframe>
function downloadFile(filePath){ var fileName=encodeURI(filePath); document.getElementById("hidden_frame").src = "JS/StreetMap/downloadFile.jsp?fileName="+fileName; }
public void downloadFile() { HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = getRequest(); response.setCharacterEncoding("GBK"); String fileUrl=request.getParameter("fileUrl"); String fileName=request.getParameter("fileName"); String cmsAttachRealFile = ""; try { fileUrl = new String(fileUrl.getBytes("ISO8859-1"),"GBK"); cmsAttachRealFile = FilePathSptUtil.UPLOAD_ROOT_PATH //+ FilePathSptUtil.UPLOAD_CMS + fileUrl; fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8"); fileName = URLEncoder.encode(fileName, "UTF-8"); } catch (Exception e1) { // TODO 附件流下载 弹出下载框 e1.printStackTrace(); } try { OutputStream o = response.getOutputStream(); byte b[] = new byte[500]; File fileLoad = new File(cmsAttachRealFile); response.setContentType("application/octet-stream"); response.setHeader("content-disposition", "attachment; filename=\"" + fileName + "\""); long fileLength = fileLoad.length(); String length1 = String.valueOf(fileLength); response.setHeader("Content_Length", length1); FileInputStream in = new FileInputStream(fileLoad); int n; while ((n = in.read(b)) != -1) { o.write(b, 0, n); } in.close(); o.close(); } catch (Exception e) { // TODO: handle exception } } //jsp response.setCharacterEncoding("UTF-8"); String fileName=request.getParameter("fileName"); String applicationPath = application.getRealPath(""); String filePath =applicationPath; String suffixName = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length()).toLowerCase(); String realFilePath = ""; try { // fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8"); realFilePath = filePath+fileName;//附件地址 File fileLoad = new File(realFilePath); if(!fileLoad.exists()){ System.out.println("没有您要打开的文件"); }else { // fileName = URLEncoder.encode(fileName, "UTF-8"); String realFileName = fileName.substring(fileName.lastIndexOf("/")+1,fileName.length()); realFileName = URLEncoder.encode(realFileName, "UTF-8"); OutputStream o = response.getOutputStream(); byte b[] = new byte[512]; //contentType设定 String contentType = ZipUtil.getContentType(suffixName); response.setContentType(contentType); response.setContentType("application/octet-stream"); //attachment表示网页会出现保存、打开对话框;inline 直接页面打开 response.setHeader("content-disposition","attachment; filename=\"" + realFileName + "\""); //response.setHeader("content-disposition","inline; filename=\"" + fileName + "\""); long fileLength = fileLoad.length(); String length1 = String.valueOf(fileLength); response.setHeader("Content_Length", length1); FileInputStream in = new FileInputStream(fileLoad); int n; while ((n = in.read(b)) != -1) { o.write(b, 0, n); } in.close(); o.close(); } } catch (Exception e) { e.printStackTrace(); }