简单的servlet上传文件
Posted 山神笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的servlet上传文件相关的知识,希望对你有一定的参考价值。
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上传文件的主要内容,如果未能解决你的问题,请参考以下文章