KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > infra > FileTransfer


1 package org.javabb.infra;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Enumeration JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
11
12 /**
13  * @author Dalton Camargo
14  */

15 public class FileTransfer {
16
17     /**
18      * Classe para enviar upload através de uma action
19      * @param multiWrapper -
20      * @param inputNameFile - Nome do arquivo a ser gravado no F.System
21      * @return - Nomes dos arquivos enviados
22      * Exemplo de utilização através da Action:
23      *
24      * String nameFile = "arquivo_" + editorial2.getIdEditorial();
25      *
26      * MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
27      * String arquivo = FileTransfer.verificaUpload(multiWrapper,nameFile);
28      * if(arquivo != null && !arquivo.equals("")){
29      * arquivo = "/diretorio/Uploads/" + nameFile + "." + arquivo;
30      * }
31      *
32      * editorial2.setImage(arquivo);
33      */

34     public static ArrayList JavaDoc uploadFile(MultiPartRequestWrapper multiWrapper) {
35         String JavaDoc fileName = "";
36         ArrayList JavaDoc filesExt = new ArrayList JavaDoc();
37         
38         if (multiWrapper.hasErrors()) {
39             Collection JavaDoc errors = multiWrapper.getErrors();
40             Iterator JavaDoc i = errors.iterator();
41             while (i.hasNext()) {
42                 //obj.addActionError((String) i.next());
43
}
44         } else {
45             Enumeration JavaDoc e = multiWrapper.getFileNames();
46             while (e.hasMoreElements()) {
47                 
48                 String JavaDoc inputValue = (String JavaDoc) e.nextElement();
49                 
50                 String JavaDoc contentType = multiWrapper.getContentType(inputValue);
51                 
52                 if(contentType != null) {
53                     fileName = multiWrapper.getFilesystemName(inputValue);
54                     
55                     File JavaDoc file = multiWrapper.getFile(inputValue);
56     
57                     String JavaDoc nameFile = file.getName();
58                     String JavaDoc pathFile = file.getAbsolutePath().replaceAll(nameFile, "");
59
60                     String JavaDoc nameWithoutDot = nameFile.substring(0, nameFile.indexOf('.'));
61                     
62                     String JavaDoc extension = "";
63                     int whereDot = nameFile.lastIndexOf( '.' );
64                     if ( 0 < whereDot && whereDot <= nameFile .length()-2 ){
65                        extension = nameFile.substring(whereDot+1 );
66                     }
67                     String JavaDoc inputNameFile = new String JavaDoc(Double.toString(System.currentTimeMillis()));
68                     
69                     file.renameTo(new File JavaDoc(pathFile + nameWithoutDot + "-" + inputNameFile + "." + extension));
70                     filesExt.add(nameWithoutDot + "-" + inputNameFile + "." + extension);
71                 } else {
72                     //Não foi selecionado nenhum arquivo
73
filesExt.add(null);
74                 }
75                 
76             }
77         }
78         return filesExt;
79     }
80
81     /**
82      * Classe para enviar upload através de uma action
83      * @param multiWrapper -
84      * @param inputNameFile - Nome do arquivo a ser gravado no F.System
85      * @return - HashMap contendo como chave o nome do arquivo
86      * enviado, e como valor o tamanho do arquivo
87      * Exemplo de utilização através da Action:
88      */

89     public static HashMap JavaDoc uploadFileRecursive(MultiPartRequestWrapper multiWrapper) {
90         String JavaDoc fileName = "";
91         HashMap JavaDoc filesExt = new HashMap JavaDoc();
92         
93         if (multiWrapper.hasErrors()) {
94             Collection JavaDoc errors = multiWrapper.getErrors();
95             Iterator JavaDoc i = errors.iterator();
96             while (i.hasNext()) {
97                 //obj.addActionError((String) i.next());
98
}
99         } else {
100             Enumeration JavaDoc e = multiWrapper.getFileNames();
101             while (e.hasMoreElements()) {
102                 
103                 String JavaDoc inputValue = (String JavaDoc) e.nextElement();
104                 
105                 String JavaDoc contentType = multiWrapper.getContentType(inputValue);
106                 
107                 if(contentType != null) {
108                     fileName = multiWrapper.getFilesystemName(inputValue);
109                     
110                     File JavaDoc file = multiWrapper.getFile(inputValue);
111     
112                     String JavaDoc nameFile = file.getName();
113                     String JavaDoc pathFile = file.getAbsolutePath().replaceAll(nameFile, "");
114                     String JavaDoc nameWithoutDot = nameFile.substring(0, nameFile.indexOf('.'));
115                     
116                     String JavaDoc extension = "";
117                     int whereDot = nameFile.lastIndexOf( '.' );
118                     if ( 0 < whereDot && whereDot <= nameFile .length()-2 ){
119                        extension = nameFile.substring(whereDot+1 );
120                     }
121                     String JavaDoc inputNameFile = new String JavaDoc(Double.toString(Math.random() + System.currentTimeMillis()));
122                     
123                     long fileLenght = (file.length()>=1024)?(file.length()/1024):(1024/file.length());
124                     //file.renameTo(new File(pathFile + inputNameFile + "." + extension));
125

126                     file.renameTo(new File JavaDoc(pathFile + nameWithoutDot + "-" + inputNameFile + "." + extension));
127                     filesExt.put(nameWithoutDot + "-" + inputNameFile + "." + extension, new Long JavaDoc(fileLenght));
128                 }
129                 
130             }
131         }
132         return filesExt;
133     }
134     
135     
136 }
137
Popular Tags