簡單的servlet上傳文件
阿新 • • 發佈:2018-05-15
smu red tst servle ont max exceptio AR 簡單的
boolean multipartContent = ServletFileUpload.isMultipartContent(request);
if (multipartContent==true) {
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
upload.setSizeMax(40 * 1024 * 1024);
List<FileItem> fileItemList = upload.parseRequest(request);
for (FileItem fileItem : fileItemList) {
File file1 = new File("E:\\app\\" + fileItem.getName());
try {
fileItem.write(file1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.sendRedirect("/hello.jsp");
maven配置:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
簡單的servlet上傳文件