DWR文件上传和下载
项目里用到文件上传,且要求页面无刷新,用DWR可以很方便地实现。下面简单说说:
1.单文件上传
HTML里:
<input type="file" id="uploadFile" > <a href="#" id="upload" class="btn_small_wraper" onclick="uploadFile();">Upload</a>Js代码:
function uploadFile(){ if($("#uploadFile").val()==""){ alertMessagesBox("alert-messages-box","File path is mandatory!",false,null);//自定义的alert提示控件 return false; } rightPaneOverlay($("#tabs-report-maintain")); //加一个jquery的overlay var uploadFile = dwr.util.getValue("uploadFile"); var fileNames = uploadFile.value.split("\"); var fileName = fileNames[fileNames.length-1]; JsReportAction.uploadFile(uploadFile,fileName,function(data){ $("#rightOverlay").remove(); $("#uploadFile).val(""); if(data.errMessage!=null && data.errMessage!=""){//failed alertMessagesBox("alert-messages-box",data.errMessage,true,"Uploading Failed"); return false; }else{//success promptMessagesBox("alert-messages-box","The file has been uploaded successfully.",false,null); }); }后台:
public static ReportForm uploadFile(InputStream uploadFile, String fileName, String uploadPath) { ReportForm form = new ReportForm(); WebContext webContext = WebContextFactory.get(); // String relativePath = webContext.getContextPath() + uploadPath // + File.separator; String actualPath = webContext.getHttpServletRequest().getSession() .getServletContext().getRealPath(uploadPath + File.separator); File file = null; FileOutputStream foutput = null; try { file = new File(actualPath + File.separator + fileName); if (file != null && file.exists()) { form.setErrMessage("There is another file with the same name."); return form; } foutput = new FileOutputStream(file); Streams.copy(uploadFile, foutput, true); // start to write file } catch (Exception e) { e.printStackTrace(); form.setErrMessage(e.getMessage()); } finally { try { if (foutput != null) { foutput.close(); } if (uploadFile != null) { uploadFile.close(); } } catch (IOException e) { e.printStackTrace(); } } form.setFileName(fileName); return form; }多文件道理是一样的,类似实现。
2.文件下载
Html里:
<span id="file_name_div" style="cursor:pointer;" onclick="downloadFile();" title="Download"></span>Js代码:
function downloadFile(){ var fileName = $("#file_name_div").html(); JsReportAction.downloadFile(fileName,function (data){ dwr.engine.openInDownload(data); }); }后台:
public FileTransfer downloadFile(String fileName, HttpServletRequest request, HttpServletResponse response, ServletContext context) { String realPath = context.getRealPath("/WEB-INF/common_report/" + fileName); File file = null; FileInputStream fis = null; try { file = new File(realPath); fis = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } //InputStreamFactory inputStreamFactory = new SimpleInputStreamFactory(fileInputStream); String reportName = fileName.substring(0, fileName.length() - 6); return new FileTransfer(reportName + ".jrxml", "text/plain", file.length(),fis); //FileTransfer为DWR的一个接口。可以接受inputStream,inputStreamFactory和btye[]类型,相当的好用。 }
Now game over!
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: SugarOrm使用
- 下一篇: spring在service层获取session和request