KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > designer > FileUploader


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

16 package org.apache.jetspeed.modules.actions.portlets.designer;
17
18
19 import java.io.File JavaDoc;
20 import org.apache.turbine.util.upload.FileItem;
21
22 //for logging
23
import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * File Uploader helper
28  *
29  * @author <a HREF="mailto:jlim@gluecode.com">Jonas Lim</a>
30  * @version $Id: FileUploader.java,v 1.1 2004/03/10 22:53:59 taylor Exp $
31  */

32 public class FileUploader
33 {
34
35     private static Log log = LogFactory.getLog(FileUploader.class);
36
37     public boolean upload(FileItem fileItem, String JavaDoc location,
38             String JavaDoc fileTypes[])
39     {
40         boolean hasUpload = false;
41
42         try
43         {
44             File JavaDoc file = new File JavaDoc(fileItem.getFileName());
45             String JavaDoc filename = file.getName();
46             String JavaDoc contentType = fileItem.getContentType();
47
48             int index = filename.lastIndexOf("\\");
49             int index2 = filename.lastIndexOf("//");
50
51             if (index > 0)
52             {
53                 filename = filename.substring(index + 1);
54             }
55
56             if (index2 > 0)
57             {
58                 filename = filename.substring(index2 + 1);
59             }
60             // remove restrictions in upload
61
/*
62             boolean isFileType = false;
63             for (int i = 0; i < fileTypes.length; i++)
64             {
65                 if (contentType.equalsIgnoreCase(fileTypes[i]))
66                 {
67                     isFileType = true;
68                     break;
69                 }
70             }
71
72             if (isFileType == true || fileTypes == null
73                     || fileTypes.length == 0)
74             {
75                 fileItem.write(location + filename);
76                 hasUpload = true;
77             }
78           */

79             
80             fileItem.write(location + filename);
81             hasUpload = true;
82         } catch (Exception JavaDoc e)
83         {
84             log.info("error in FileUploader class");
85             hasUpload = false;
86             log.error(e);
87         }
88         return hasUpload;
89     }
90
91     public String JavaDoc getFilename(FileItem fileItem, String JavaDoc location,
92             String JavaDoc fileTypes[])
93     {
94         String JavaDoc filename = "no result";
95         try
96         {
97             File JavaDoc file = new File JavaDoc(fileItem.getFileName());
98             filename = fileItem.getName();
99
100             int index = filename.lastIndexOf("\\");
101             int index2 = filename.lastIndexOf("//");
102
103             if (index > 0)
104             {
105                 filename = filename.substring(index + 1);
106             }
107
108             if (index2 > 0)
109             {
110                 filename = filename.substring(index2 + 1);
111             }
112             filename = location + filename;
113         } catch (Exception JavaDoc e)
114         {
115             log.error(e);
116         }
117         return filename;
118     }
119 }
Popular Tags