1 package org.appfuse.webapp.action; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileOutputStream ; 6 import java.io.InputStream ; 7 import java.io.OutputStream ; 8 9 import org.appfuse.Constants; 10 11 import com.opensymphony.webwork.ServletActionContext; 12 import com.opensymphony.xwork.ValidationAware; 13 14 public class FileUploadAction extends BaseAction implements ValidationAware { 15 private static final long serialVersionUID = -9208910183310010569L; 16 private File file; 17 private String fileContentType; 18 private String fileFileName; 19 private String name; 20 21 public String execute() throws Exception { 22 if (this.cancel != null) { 23 return "cancel"; 24 } 25 26 if (file == null || file.length() > 2097152) { 27 addActionError(getText("maxLengthExceeded")); 28 return INPUT; 29 } 30 31 String uploadDir = 33 ServletActionContext.getServletContext().getRealPath("/resources") + 34 "/" + getRequest().getRemoteUser() + "/"; 35 36 File dirPath = new File (uploadDir); 38 39 if (!dirPath.exists()) { 40 dirPath.mkdirs(); 41 } 42 43 InputStream stream = new FileInputStream (file); 45 46 OutputStream bos = new FileOutputStream (uploadDir + fileFileName); 48 int bytesRead = 0; 49 byte[] buffer = new byte[8192]; 50 51 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { 52 bos.write(buffer, 0, bytesRead); 53 } 54 55 bos.close(); 56 stream.close(); 57 58 59 getRequest().setAttribute("location", dirPath.getAbsolutePath() 61 + Constants.FILE_SEP + fileFileName); 62 63 String link = 64 getRequest().getContextPath() + "/resources" + "/" + 65 getRequest().getRemoteUser() + "/"; 66 67 getRequest().setAttribute("link", link + fileFileName); 68 69 return SUCCESS; 70 } 71 72 public String doDefault() { 73 return INPUT; 74 } 75 76 public void setFile(File file) { 77 this.file = file; 78 } 79 80 public void setFileContentType(String fileContentType) { 81 this.fileContentType = fileContentType; 82 } 83 84 public void setFileFileName(String fileFileName) { 85 this.fileFileName = fileFileName; 86 } 87 88 public void setName(String name) { 89 this.name = name; 90 } 91 92 public String getName() { 93 return name; 94 } 95 96 public File getFile() { 97 return file; 98 } 99 100 public String getFileContentType() { 101 return fileContentType; 102 } 103 104 public String getFileFileName() { 105 return fileFileName; 106 } 107 } 108 | Popular Tags |