DWR文件上传和下载

项目里用到文件上传,且要求页面无刷新,用DWR可以很方便地实现。下面简单说说:
1.单文件上传
HTML里:


Upload
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里:
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!






你可能感兴趣的:(DWR)