使用ServletFileUpload的parseRequest上传文件?

我上传了inputtype="file"在Web应用程序中浏览的文件。问题是我可以将FileItem列表大小设为0,尽管我可以在下面看到所有上传的文件信息

request-> JakartaMutltiPartRequest->文件属性

这是读取文件的Java代码

public InputStream parseRequestStreamWithApache(HttpServletRequest request)

throws FileUploadException, IOException {

InputStream is = null;

FileItemFactory factory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(factory);

List items = upload.parseRequest(request);

// here the item size is 0 ,i am not sure why i am not getting my file upload in browser with type="file"

// but If inspect request in debugger i can see my file realted info in request--->JakartaMutltiPartRequest----->files attribute

Iterator iter = items.iterator();

while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (!item.isFormField()) {

is = item.getInputStream();

}

}

return is;

}

<form NAME="form1" action="customer/customerManager!parseRequestStreamWithApache.action" ENCTYPE="multipart/form-data"   method="post" >

<TABLE >

<tr>

<th>Upload File</th>

<td>

<input name="fileUploadAttr" id="filePath" type="file" value="">

</td>

<td >

<Input type="submit" value ="uploadFile"/>

</td>

</tr>

</TABLE>

</form>

回答:

正如我在您之前发布的对同一个问题的评论中所说,这很可能是因为您之前已经解析了请求。这些文件是请求正文的一部分,您只能解析一次。

我通常以这种方式使用commons-upload:

if (ServletFileUpload.isMultipartContent(request)) {

ServletFileUpload fileUpload = new ServletFileUpload();

FileItemIterator items = fileUpload.getItemIterator(request);

// iterate items

while (items.hasNext()) {

FileItemStream item = items.next();

if (!item.isFormField()) {

is = item.openStream();

}

}

}

以上是 使用ServletFileUpload的parseRequest上传文件? 的全部内容, 来源链接: utcz.com/qa/421368.html

回到顶部