1 16 package org.apache.jetspeed.modules.actions.portlets.designer; 17 18 19 import java.io.File ; 20 import org.apache.turbine.util.upload.FileItem; 21 22 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 32 public class FileUploader 33 { 34 35 private static Log log = LogFactory.getLog(FileUploader.class); 36 37 public boolean upload(FileItem fileItem, String location, 38 String fileTypes[]) 39 { 40 boolean hasUpload = false; 41 42 try 43 { 44 File file = new File (fileItem.getFileName()); 45 String filename = file.getName(); 46 String 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 79 80 fileItem.write(location + filename); 81 hasUpload = true; 82 } catch (Exception 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 getFilename(FileItem fileItem, String location, 92 String fileTypes[]) 93 { 94 String filename = "no result"; 95 try 96 { 97 File file = new File (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 e) 114 { 115 log.error(e); 116 } 117 return filename; 118 } 119 } | Popular Tags |