我正在使用apache commons fileupload stream api。但是FileItemIterator FileItemIterator iter = upload.getItemIterator(request);
在其hasNext()iter.hasNext()
中返回false这有什么问题?
代码和Web部分如下:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
/**
* Apache commons file upload method will be used
*/
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
try {
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
// Process the input stream
//...
}
}
} catch (FileUploadException ex) {
Logger.getLogger(ResourceUploadServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
jsp页面如下:
<form action="AServlet" method="POST"
enctype="multipart/form-data">
<input type="file" name="Content" />
Description : <input type="text" name="Description" />
<input type="submit" value="Submit" />
</form>
最佳,
答案 0 :(得分:2)
在我的web.xml文件中。有一个过滤器
<filter>
<filter-name>resourceUploadServlet</filter-name>
<filter-class>org.mortbay.servlet.MultiPartFilter</filter-class>
<init-param>
<param-name>maxSize</param-name>
<param-value>2147483648</param-value>
</init-param>
</filter>
当我删除过滤器时,问题就解决了......
答案 1 :(得分:0)
可能值得检查的一些事情:
PS:最好使用Logger类对“表单和文件”字段解析是否完成进行“双重确认”。