1 package org.appfuse.webapp.action; 2 3 import java.io.File ; 4 import java.io.FileOutputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.OutputStream ; 8 import java.io.Serializable ; 9 10 import javax.servlet.http.HttpServletRequest ; 11 12 import org.apache.myfaces.custom.fileupload.UploadedFile; 13 14 import org.appfuse.Constants; 15 16 public class FileUpload extends BasePage implements Serializable { 17 private static final long serialVersionUID = 6932775516007291334L; 18 private UploadedFile file; 19 private String name; 20 21 public UploadedFile getFile() { 22 return file; 23 } 24 25 public void setFile(UploadedFile file) { 26 this.file = file; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public String upload() throws IOException { 38 HttpServletRequest request = getRequest(); 39 40 String uploadDir = 43 getServletContext().getRealPath("/resources") + "/" + 44 request.getRemoteUser() + "/"; 45 46 File dirPath = new File (uploadDir); 48 49 if (!dirPath.exists()) { 50 dirPath.mkdirs(); 51 } 52 53 InputStream stream = file.getInputStream(); 55 56 OutputStream bos = 58 new FileOutputStream (uploadDir + file.getName()); 59 int bytesRead = 0; 60 byte[] buffer = new byte[8192]; 61 62 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { 63 bos.write(buffer, 0, bytesRead); 64 } 65 66 bos.close(); 67 68 stream.close(); 70 71 request.setAttribute("friendlyName", name); 73 request.setAttribute("fileName", file.getName()); 74 request.setAttribute("contentType", file.getContentType()); 75 request.setAttribute("size", file.getSize() + " bytes"); 76 request.setAttribute("location", 77 dirPath.getAbsolutePath() + Constants.FILE_SEP + 78 file.getName()); 79 80 String link = 81 request.getContextPath() + "/resources" + "/" + 82 request.getRemoteUser() + "/"; 83 84 request.setAttribute("link", link + file.getName()); 85 86 return "success"; 87 } 88 } 89 | Popular Tags |