I have a form like this :
<form id="test" name="test" action="/pages/font/getFontTitle.jsp" method="POST" enctype="multipart/form-data">
<div class="dialog">
<table>
<tbody>
<tr class="prop"><td valign="top">Name</td><td><input type="text" name="name"></td></tr>
<tr class="prop"><td valign="top"><input type="file" name="fname" size="62" value="" id="fname"/></td><td><span class="button"><s:submit/></span></td></tr>
</tbody>
</table>
</div>
</form>
And my servlet look like this :
String contentType=request.getContentType(),Location="0.";
out.println("Done");
System.out.println("contentType = "+contentType);
boolean isMultipart=ServletFileUpload.isMultipartContent(request); // Check that we have a file upload request
System.out.println("isMultipart = "+isMultipart);
int formDataLength=request.getContentLength(); // We are taking the length of Content type data
byte dataBytes[]=new byte[formDataLength];
System.out.println("dataBytes = "+dataBytes.length+"\n"+dataBytes.toString());
java.util.Map<java.lang.String,java.lang.String[]> ParameterMap=request.getParameterMap();
Iterator it=ParameterMap.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pair=(Map.Entry)it.next();
System.out.println(pair.getKey()+" = "+(pair.getValue()).toString());
// it.remove(); // avoids a ConcurrentModificationException
}
System.out.println("request = \n"+request.getParameterNames().toString());
if (isMultipart)
{
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
List items=null;
Location+="1.";
try { items=upload.parseRequest(request); }
catch (FileUploadException e) { e.printStackTrace(); }
Location+="2.";
Iterator itr=items.iterator();
Location+="3.";
while (itr.hasNext())
{
Location+="4.";
FileItem item=(FileItem)itr.next();
if (item.isFormField())
{
String name=item.getFieldName(),value=item.getString();
System.out.println("name = "+name+" value = "+value);
Location+="5.";
}
else
{
try
{
Location+="6.";
String itemName=item.getName(),savedFilePath=itemName;
File savedFile=new File(savedFilePath);
System.out.println("savedFile = "+savedFile.getAbsolutePath());
item.write(savedFile);
}
catch (Exception e) { e.printStackTrace(); }
}
}
}
Location+="e.";
out.println(" [ "+Location+" ]");
out.flush();
The result I get was :
contentType = multipart/form-data; boundary=---------------------------15890672924370
isMultipart = true
dataBytes = 35909
[B@14d3ba9
name = [Ljava.lang.String;@187f9d7
request =
java.util.Vector$1@23ca6e
dataBytes has a file in it, but it never got into the while (itr.hasNext()) loop, the jsp output was : Done [ 0.1.2.3.e. ] , Location 4,5,6 was never reached.
[1] Why ? [2] How to turn "dataBytes = [B@14d3ba9 " into something readable or a file ? [3] How to turn "name = [Ljava.lang.String;@187f9d7" into the original string value ? [4] I'm using "(pair.getValue()).toString()", but why it's not a readable string ?