要回答我的問題,並作出長話短說,這個代碼片段的伎倆:
@Controller
@RequestMapping("/FileUploadController")
public class FileUploadController {
@RequestMapping(method = RequestMethod.POST)
protected ModelAndView onSubmit(HttpServletRequest request) throws Exception{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> /* FileItem */ items = upload.parseRequest(request);
for (FileItem fileItem : items) {
if (fileItem.isFormField()) {
// processFormField(fileItem);
} else {
File uploadedFile = new File("/tmp/junk/" + fileItem.getName());
fileItem.write(uploadedFile);
}
}
return new ModelAndView("index");
}
}
這個例子使用Spring,但你應該能夠做到完全一樣長因爲你有HttpServletRequest對象。