Dwr File Upload
Configure FileService service ing:
Dwr. xml
FileService:
package com.demo.service;import java.io.File;import java.io.IOException;import java.io.InputStream;import org.apache.commons.io.FileUtils;import org.apache.commons.io.FilenameUtils;import org.directwebremoting.WebContext;import org.directwebremoting.WebContextFactory;public class FileService {public String upload(InputStream inputStream,String fileName) throws IOException{String tempFileName=FilenameUtils.getName(fileName);String path=getRealPath("upload");File file=new File(path+File.separator+tempFileName);FileUtils.copyInputStreamToFile(inputStream, file);return file.getPath();}public String getRealPath(String dir){WebContext context=WebContextFactory.get();return context.getSession().getServletContext().getRealPath(dir);}}
Upload. jsp:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
File Upload<Script type = "text/javascript" src = "<% = request. getContextPath () %>/dwr/engine. js "> </script> <script type =" text/javascript "src =" <% = request. getContextPath () %>/dwr/util. js "> </script> <script type =" text/javascript "src =" <% = request. getContextPath () %>/dwr/interface/FileService. js "> </script> <script type =" text/javascript "> function upload () {var file = dwr. util. getValue ("file"); FileService. upload (file, file. value, function (result) {alert (result) ;}</script>Attachment:
Upload
Result:
The attachment has been uploaded to the upload folder.