1 package org.javabb.infra; 2 3 import java.io.File ; 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.Enumeration ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 10 import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper; 11 12 15 public class FileTransfer { 16 17 34 public static ArrayList uploadFile(MultiPartRequestWrapper multiWrapper) { 35 String fileName = ""; 36 ArrayList filesExt = new ArrayList (); 37 38 if (multiWrapper.hasErrors()) { 39 Collection errors = multiWrapper.getErrors(); 40 Iterator i = errors.iterator(); 41 while (i.hasNext()) { 42 } 44 } else { 45 Enumeration e = multiWrapper.getFileNames(); 46 while (e.hasMoreElements()) { 47 48 String inputValue = (String ) e.nextElement(); 49 50 String contentType = multiWrapper.getContentType(inputValue); 51 52 if(contentType != null) { 53 fileName = multiWrapper.getFilesystemName(inputValue); 54 55 File file = multiWrapper.getFile(inputValue); 56 57 String nameFile = file.getName(); 58 String pathFile = file.getAbsolutePath().replaceAll(nameFile, ""); 59 60 String nameWithoutDot = nameFile.substring(0, nameFile.indexOf('.')); 61 62 String extension = ""; 63 int whereDot = nameFile.lastIndexOf( '.' ); 64 if ( 0 < whereDot && whereDot <= nameFile .length()-2 ){ 65 extension = nameFile.substring(whereDot+1 ); 66 } 67 String inputNameFile = new String (Double.toString(System.currentTimeMillis())); 68 69 file.renameTo(new File (pathFile + nameWithoutDot + "-" + inputNameFile + "." + extension)); 70 filesExt.add(nameWithoutDot + "-" + inputNameFile + "." + extension); 71 } else { 72 filesExt.add(null); 74 } 75 76 } 77 } 78 return filesExt; 79 } 80 81 89 public static HashMap uploadFileRecursive(MultiPartRequestWrapper multiWrapper) { 90 String fileName = ""; 91 HashMap filesExt = new HashMap (); 92 93 if (multiWrapper.hasErrors()) { 94 Collection errors = multiWrapper.getErrors(); 95 Iterator i = errors.iterator(); 96 while (i.hasNext()) { 97 } 99 } else { 100 Enumeration e = multiWrapper.getFileNames(); 101 while (e.hasMoreElements()) { 102 103 String inputValue = (String ) e.nextElement(); 104 105 String contentType = multiWrapper.getContentType(inputValue); 106 107 if(contentType != null) { 108 fileName = multiWrapper.getFilesystemName(inputValue); 109 110 File file = multiWrapper.getFile(inputValue); 111 112 String nameFile = file.getName(); 113 String pathFile = file.getAbsolutePath().replaceAll(nameFile, ""); 114 String nameWithoutDot = nameFile.substring(0, nameFile.indexOf('.')); 115 116 String extension = ""; 117 int whereDot = nameFile.lastIndexOf( '.' ); 118 if ( 0 < whereDot && whereDot <= nameFile .length()-2 ){ 119 extension = nameFile.substring(whereDot+1 ); 120 } 121 String inputNameFile = new String (Double.toString(Math.random() + System.currentTimeMillis())); 122 123 long fileLenght = (file.length()>=1024)?(file.length()/1024):(1024/file.length()); 124 126 file.renameTo(new File (pathFile + nameWithoutDot + "-" + inputNameFile + "." + extension)); 127 filesExt.put(nameWithoutDot + "-" + inputNameFile + "." + extension, new Long (fileLenght)); 128 } 129 130 } 131 } 132 return filesExt; 133 } 134 135 136 } 137 | Popular Tags |