最近开发中用到sturts2的文件下载,刚开始用的本地下载,上传服务器后发现文件服务器和应用服务器不在同一地址.就会导致下载失败,于是全部改成用url下载的方式下载,并增加下载失败后弹出提示框的操作.
1.download.jsp代码,downloadFileIframe用于文件不存在时弹出提示框
<body> <head> <script type="text/javascript"> function fileDownload(xpath){ $("#xpath").val(xpath); //window.location.href(""+xpath); document.download.submit(); } </script> </head> ................. <a href="javascript:fileDownload('assetPath');" style="border: 0"> <img src="img/xiaz.jpg" /> </a> <form action="fileDownload_downloadLocal.action" name="download" method="post" enctype="multipart/form-data" target="downloadFileIframe"> <input name="path" id="xpath" type="hidden"/> <iframe name="downloadFileIframe" style="display:none"></iframe> </form> ............. </body>
2.nofile.jsp 代码,用于文件不存在时提示信息弹出框
<body> ................... <head> <script type="text/javascript"> var downloadFileMessage ="<%=request.getAttribute("downloadFileMessage")%>"; if(downloadFileMessage != "null"){ alert(downloadFileMessage); } </script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> ................. <body>
3.struts2 配置文件
<!--下载资源action --> <action name="fileDownload_*" class="com.zhl.web.resourcecenter.action.FileDownloadAction" method="{1}"> <result name="success" type="stream"> <param name="contentCharSet">UTF-8</param> <param name="contentType">application/x-download</param> <param name="inputName">downloadStream</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">4096</param> </result> <result name="error">/resource_center/nofile.jsp</result> </action>
4.java下载代码
public class FileDownloadAction extends ActionSupport implements ServletRequestAware{ private static final long serialVersionUID = 3732213485796116174L; private String path;// 要下载的文件路径 private InputStream downloadStream;// 输出流 private String fileName;// 输出流生成的文件名 private String downloadFileMessage; public String getDownloadFileMessage() { return downloadFileMessage; } public void setDownloadFileMessage(String downloadFileMessage) { this.downloadFileMessage = downloadFileMessage; } public InputStream getDownloadStream() { return downloadStream; } public void setDownloadStream(InputStream downloadStream) { this.downloadStream = downloadStream; } public String getFileName() { return fileName; }public String downloadLocal() { String result = "success"; URL url = null; HttpURLConnection connect = null; try { fileName = path.substring(path.lastIndexOf("/") + 1, path.length()); fileName = URLEncoder.encode(fileName, "utf-8");
path = path.replaceAll(" ", "%20");
//path like http://192.168.0.2/test/test.txt url = new URL(path); connect = (HttpURLConnection) url.openConnection(); connect.setConnectTimeout(5000); downloadStream = connect.getInputStream(); } catch (Exception e) { downloadFileMessage = "对不起,您下载的文件不存在或者被损坏,请重新选择其他文件下载!"; result = "error"; e.printStackTrace(); } finally { connect = null; url = null; } return result; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public void setFileName(String fileName) { this.fileName = fileName; } @Override public void setServletRequest(HttpServletRequest arg0) { // TODO Auto-generated method stub }