0

I am having a form to upload file elements. Form is below :

<FORM name="dcdipap" enctype="multipart/form-data" method="post"> 
                            <input id="realfile1" type="file" style="position:absolute;visibility:hidden;"
onchange="document.getElementById('dummyfile1').value=this.value;if(validate())preview(this, '1');return true;">
<table cellpadding=0 cellspacing=0 border=0>
  <tr><td>
<input id="dummyfile1" type="text" >&nbsp;&nbsp;
</td>
    <td id="submitHead" class="buttonRegHead">&nbsp;</td>
    <td id="submitBody" align=center class="buttonRegBody"> 
    <a class=buttonLabel href="#" onclick="document.getElementById('realfile1').click();return false;" >Browse</a>
    </td>
    <td id="submitTail" class="buttonRegTail">&nbsp;</td>
  </tr>

When a file is selected and the form is submitted, servlet is called.My code in the servlet is as below :

List<Object> items=null;
items = upload.parseRequest(req);
Iterator<?> iter = items.iterator();
FileItem item;
InputStream fileData = null;    
String FilePath="";
while (iter.hasNext())
{
item = (FileItem) iter.next();
if(!item.isFormField()){
filePath = item.getName();
fileData = item.getInputStream();
}
}

EDIT TO THE QUESTION : I have made some error in the code. When I submit the above form, in the servlet, its not even going inside the loop
if(!item.isFormField()) { }

Because of this I am not able to get file name and the the file Data also. Can any one please tell me why it is like that,even though I have declared

<input type="file"/>
Nagesh
  • 434
  • 1
  • 8
  • 26

1 Answers1

0

After reviewing your code, I suppose that your upload object is a ServletFileUpload class from Apache Commons File Upload.

The Javadoc of the FileItem class lists the following methods which are probably what you are looking for:

  • getFieldName()
  • getSize()

Add in your Java code something like that:

long fileSize = item.getSize();
String fileName = item.getFieldName();
Emmanuel Keller
  • 3,384
  • 1
  • 14
  • 16