java实现文件上传时遇到的问题

局座 发布于 2015/05/27 22:13
阅读 486
收藏 0

最近使用java实现文件上传的时候,获取到的list为空,应该如何解决??代码如下

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");

DiskFileItemFactory factory = new DiskFileItemFactory();
//获取绝对路径
String path  = request.getRealPath("/upload");
//
factory.setRepository(new File(path));
factory.setSizeThreshold(1024*1024);
//
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> list = (List<FileItem>)upload.parseRequest(request);
System.out.println(list.size());
for (FileItem fileItem : list) {
System.out.println(fileItem.getFieldName());
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

加载中
返回顶部
顶部