我有这样的表格:
<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>
我的 servlet 看起来像这样:
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();
我得到的结果是:
contentType = multipart/form-data; boundary=---------------------------15890672924370
isMultipart = true
dataBytes = 35909
[B@14d3ba9
name = [Ljava.lang.String;@187f9d7
request =
java.util.Vector$1@23ca6e
dataBytes 中有一个文件,但它从未进入 while (itr.hasNext()) 循环,jsp 输出为:Done [ 0.1.2.3.e. ] ,从未到达位置 4,5,6。
[1] 为什么?[2] 如何将“dataBytes = [B@14d3ba9”变成可读的东西或文件?[3] 如何将“name = [Ljava.lang.String;@187f9d7”转为原始字符串值?[4] 我正在使用“(pair.getValue()).toString()”,但为什么它不是可读字符串?