KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > authoring > UploadHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance with the License. You may obtain
4  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
5  * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
6  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
7  * for the specific language governing permissions and limitations under the License.
8  */

9
10 /* $Id: UploadHelper.java 46183 2004-09-16 14:54:13Z michi $ */
11
12 package org.apache.lenya.cms.authoring;
13
14 import org.apache.cocoon.environment.Request;
15 import org.apache.cocoon.servlet.multipart.Part;
16 import org.apache.log4j.Category;
17
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 /**
25  * Helper class for uploading files.
26  * @author <a HREF="mailto:andreas@apache.org">Andreas Hartmann </a>
27  * @author <a HREF="mailto:michi@apache.org">Michael Wechner </a>
28  * @version $Id: UploadHelper.java 46183 2004-09-16 14:54:13Z michi $
29  */

30 public class UploadHelper {
31
32     private static final Category log = Category.getInstance(UploadHelper.class);
33
34     private File JavaDoc directory;
35
36     /**
37      * Ctor.
38      * @param directory The directory to save the files to.
39      */

40     public UploadHelper(File JavaDoc directory) {
41         this.directory = directory;
42     }
43
44     /**
45      * Ctor.
46      * @param directoryPath The path of the directory to save the files to.
47      */

48     public UploadHelper(String JavaDoc directoryPath) {
49         this.directory = new File JavaDoc(directoryPath);
50     }
51
52     /**
53      * Save uploaded file
54      * @param part The part of the multipart request.
55      * @return <code>true</code> if the upload succeeded, <code>false</code> otherwise.
56      */

57     public boolean save(Part part) {
58         File JavaDoc file = new File JavaDoc(directory, part.getFileName());
59         return save(part, file);
60     }
61
62     /**
63      * Save uploaded file
64      * @param part The part of the multipart request.
65      * @return <code>true</code> if the upload succeeded, <code>false</code> otherwise.
66      */

67     public boolean save(Part part, File JavaDoc file) {
68         if (log.isDebugEnabled()) {
69             log.debug("Uploading file: [" + file.getAbsolutePath() + "]");
70         }
71
72         if (!directory.isDirectory()) {
73             directory.mkdirs();
74             if (log.isInfoEnabled()) {
75                 log.info("Directory has been created: [" + directory + "]");
76             }
77         }
78
79         InputStream JavaDoc in = null;
80         OutputStream JavaDoc out = null;
81         try {
82             out = new FileOutputStream JavaDoc(file.getAbsolutePath());
83             in = part.getInputStream();
84             byte[] buf = new byte[4096];
85             int read = in.read(buf);
86
87             while (read > 0) {
88                 out.write(buf, 0, read);
89                 read = in.read(buf);
90             }
91         } catch (Exception JavaDoc e) {
92             log.error(e);
93             return false;
94         } finally {
95             try {
96                 if (out != null) {
97                     out.close();
98                 }
99                 if (in != null) {
100                     in.close();
101                 }
102             } catch (IOException JavaDoc e) {
103                 log.error(e);
104             }
105         }
106         return true;
107     }
108
109     /**
110      * Saves the a file the request for a certain request parameter name.
111      * @param request The request.
112      * @param requestParameter The name of the &lt;input type="file"/&gt; request parameter value.
113      * @return The saved file or <code>null</code> if the upload was not successful.
114      * @throws Exception when something went wrong.
115      */

116     public File JavaDoc save(Request request, String JavaDoc requestParameter) throws Exception JavaDoc {
117
118         Part part = (Part) request.get(requestParameter);
119
120         File JavaDoc file = null;
121
122         boolean success = save(part);
123         if (success) {
124             file = new File JavaDoc(directory, part.getFileName());
125         }
126
127         return file;
128     }
129
130 }
131
Popular Tags