Hello all,
I am trying to upload files, but I have some doubts
As I´ve read, there are two typical ways to do that:
1.Using StrutsFileUpload
MyActionForm myForm = (MyActionForm)form;
FormFile myFile = myForm.getMyFile();
String fileName = myFile.getFileName();
String filePath = "G:/FILES/";
File fileToCreate = new File(filePath, fileName);
InputStream stream = myFile.getInputStream();
BufferedInputStream in = new BufferedInputStream(stream);
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(fileToCreate));
byte[] bytes = new byte[1048576];
int len = 0;
while ( (len = in.read( bytes )) > 0 )
{
out.write( bytes, 0, len );
}
out.close();
in.close();
stream.close();
myFile.destroy();
2.Using Jakarta Commons FileUpload
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
factory.setRepository(new File("G:/FICHEROS/TEMP"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(2000000000);
List fileItems = upload.parseRequest(request);
Iterator i = fileItems.iterator();
FileItem fi = (FileItem)i.next();
String path="G:/FILES/";
String fileName = fi.getName();
int lastCharacter = fileName.lastIndexOf("\\");
String title =
fi.getName().substring(lastCharacter+1,fi.getName().length());
fi.write(new File(path, title));
Everyone has its disadvantages:
1) the struts file upload is slower than the commons file upload: until the
tmp file is completed, all is equitable. But the difference comes with lines
fi.write(new File(path, title));
and
byte[] bytes = new byte[1048576];
int len = 0;
while ( (len = in.read( bytes )) > 0 )
{
out.write( bytes, 0, len );
}
I´ve tried to upload a 200MB file and it takes 120 with commons and 25
with struts. Is there any option to make struts upload file faster?
2) The commons file upload cannot be used inside an action. It must be an
independent servlet.
As well as saving a file in the server (not in the database), I want to save
some info in the database. That is info of the file and of the person who
save it.
Here is the decision, but I don´t know which is the better one:
1) Use Struts fileUpload: Create a form with all fields I need and the input
file field and an action.
2) Use Commons fileUpload: Create a form with all fields, but the input
file. Save all the info in the database and open then the servlet with the
file field. Save the file and if something wrong happens, delete the
database info saved in the last page (if it is possible).
3) Any idea???
Thanks a lot.
Best regards,
Paco
_________________________________________________________________
Acepta el reto MSN Premium: Protección para tus hijos en internet.
Descárgalo y pruébalo 2 meses gratis.
http://join.msn.com?XAPID=1697&DI=1055&HL=Footer_mailsenviados_proteccioninfantil
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]