我正在尝试使用DiskFileItemFactory
使用java Web应用程序上传文件。但问题是它没有获取任何文件。它在迭代中显示空数组。
iter.size()
为0。
请参阅下面的代码。
public ModelAndView upload( ModelMap model, HttpSession session, HttpServletRequest request, HttpServletResponse resp) throws IOException, ServletException, FileUploadException {
if (ServletFileUpload.isMultipartContent(request)) {
System.out.println("file available");
}
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
// ResourceImpl resource = new ResourceImpl();
try {
//Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
System.out.println(items.size());
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
}
} catch (Exception e) {
}
}
我的表格是;
<form method="post" action="/DropBox/upload.html" enctype="multipart/form-data">
Select file to upload: <input type="file" name="uploadFile" />
<br/><br/>
<input type="submit" value="Upload" />
</form>
我做错了。 plaese帮助我。 thanq。
答案 0 :(得分:0)
你应该尝试每一个。它做得很好。
if (ServletFileUpload.isMultipartContent(request)) {
try {
List<FileItem> multipart = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : multipart) {
if (!item.isFormField()) {
filename = new File(item.getName()).getName();
folder = "path";
File file = new File(folder);
if (!file.exists()) {
file.mkdir();
}
}
item.write(new File(folder + "/" + filename));
}
}
}