Women in Technology

Hear us Roar



Article:
  Using the Jakarta Commons, Part 1
Subject:   FileUpload is not identifying html file element.
Date:   2006-11-20 08:59:59
From:   Rocke
Response to: FileUpload

I hava a problem in using commons upload component.
I have the foll. code in my jsp.
<html>
<body>
<form name="myform" action="/surety/UploadDocumentAttach.do"
method="post" enctype="multipart/form-data">
<input type="file" name="myfile">


<input type="submit" name="Submit" value="Submit your files"/>
</form>
</body>
</html>
In my action class,
List fileItems = upload.parseRequest(request) is returning 0. It is not identifying the html file element.
Now if I add one more html file element, then the list returned will be 1,which is the second one I added.
What could be the reason. I use struts and I followed all the changes required for this.
My action class code is:


try{
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);


// If file size exceeds, a FileUploadException will be thrown
//fu.setSizeMax(1000000);


List fileItems = upload.parseRequest(request);
System.out.println("Size of the list is "+fileItems.size());
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem fi = (FileItem) iter.next();
System.out.println(fi);
if(!fi.isFormField()){
System.out.println(fi);
String fileName = fi.getName();
// save comment and filename to database
// write the file
//fi.write(new File("C:/Rocke"));
byte[] result = null;
byte[] buffer = new byte[8196];
int len = 0;
InputStream istream = fi.getInputStream();
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
while ((len = istream.read(buffer)) != -1) {
ostream.write(buffer);
}
result = ostream.toByteArray();
istream.close();
ostream.close();
FileOutputStream outFile = new FileOutputStream("C:/Test.pdf");
outFile.write(result);
outFile.close();



}}