1 25 26 package net.killingar.actions; 27 28 import webwork.action.ActionContext; 29 import webwork.multipart.MultiPartRequestWrapper; 30 31 import java.io.File ; 32 import java.util.ArrayList ; 33 import java.util.Enumeration ; 34 import java.util.List ; 35 36 public class FileUpload extends PathActionSupport 37 { 38 40 int mFilesUploaded = 0; 42 List uploadedFiles = new ArrayList (); 43 44 public int getFilesUploaded() { return mFilesUploaded; } 46 public List getUploadedFiles() { return uploadedFiles; } 47 public void setUploadedFiles(List in) { uploadedFiles = in; } 48 49 51 public String doExecute() 53 { 54 try 55 { 56 if (ActionContext.getContext().getMultiPartRequest() == null) 57 return INPUT; 58 59 MultiPartRequestWrapper r = ActionContext.getMultiPartRequest(); 60 Enumeration names = r.getFileNames(); 61 File dir = new File (getRealPath()); 62 while (names.hasMoreElements()) 63 { 64 String foo = (String )names.nextElement(); 65 66 if (r.getFilesystemName(foo) != null) 67 { 68 File file = new File (dir, r.getFilesystemName(foo)); 69 70 if (file.exists()) 71 file.delete(); 72 73 if (r.getFile(foo) == null || !r.getFile(foo).renameTo(file)) 74 { 75 java.io.OutputStream out = new java.io.FileOutputStream (file); 76 java.io.InputStream in = r.getMemoryFileContents(foo); 77 78 if (in == null) 79 { 80 addError(foo, "failed to upload file, too big?"); 81 return ERROR; 82 } 83 84 byte[] buffer = new byte[0xFFFF]; 85 86 while (in.available() > 0) 87 { 88 int read = in.read(buffer, 0, buffer.length); 89 out.write(buffer, 0, read); 90 } 91 92 out.close(); 93 } 94 } 95 } 96 97 return SUCCESS; 98 } 99 catch (Exception e) 100 { 101 addErrorMessage("file upload failed, exception thrown ("+e.toString()+")"); 102 e.printStackTrace(); 103 104 return ERROR; 105 } 106 } 107 } 108 | Popular Tags |