KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > actions > FileUpload


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.actions;
27
28 import webwork.action.ActionContext;
29 import webwork.multipart.MultiPartRequestWrapper;
30
31 import java.io.File JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.List JavaDoc;
35
36 public class FileUpload extends PathActionSupport
37 {
38     // Types ---------------------------------------------------------
39

40     // Attributes ----------------------------------------------------
41
int mFilesUploaded = 0;
42     List JavaDoc uploadedFiles = new ArrayList JavaDoc();
43
44     // getters
45
public int getFilesUploaded() { return mFilesUploaded; }
46     public List JavaDoc getUploadedFiles() { return uploadedFiles; }
47     public void setUploadedFiles(List JavaDoc in) { uploadedFiles = in; }
48
49     // setters
50

51     // Implementation
52
public String JavaDoc doExecute()
53     {
54         try
55         {
56             if (ActionContext.getContext().getMultiPartRequest() == null)
57                 return INPUT;
58
59             MultiPartRequestWrapper r = ActionContext.getMultiPartRequest();
60             Enumeration JavaDoc names = r.getFileNames();
61             File JavaDoc dir = new File JavaDoc(getRealPath());
62             while (names.hasMoreElements())
63             {
64                 String JavaDoc foo = (String JavaDoc)names.nextElement();
65
66                 if (r.getFilesystemName(foo) != null)
67                 {
68                     File JavaDoc file = new File JavaDoc(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 JavaDoc out = new java.io.FileOutputStream JavaDoc(file);
76                         java.io.InputStream JavaDoc 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 JavaDoc e)
100         {
101             addErrorMessage("file upload failed, exception thrown ("+e.toString()+")");
102             e.printStackTrace();
103
104             return ERROR;
105         }
106     }
107 }
108
Popular Tags