使用fileupload上传文件
相关插件的安装
commons-io
官方下载地址
commons-fileupload
官方下载地址
示例
JSP完成页面展示
- 要完成文件上传操作,必须使用file组件。
- 文件上传只能使用post请求,get请求只能提交普通字符串,而且有长度限制
- 文件上传的时候form标签的enctype属性的值只能是:multipart/form-data,不能采用application/x-www-form-urlencoded,缺省值是application/x-www-form-urlencoded
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
<base href="<%=basepath%>">
<title>file up - apache commons file upload</title>
</head>
<body>
<form action="fileup.do" method="post" enctype="multipart/form-data">
用户姓名<input type="text" name="username"><br>
个人简历<input type="file" name="fileName"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>fileup</servlet-name>
<servlet-class>servlet.FileUpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fileup</servlet-name>
<url-pattern>/fileup.do</url-pattern>
</servlet-mapping>
</web-app>
Servlet
package servlet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class FileUpServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//无法再解决请求体中的乱码问题 enctype="multipart/form-data"
//request.setCharacterEncoding("UTF-8");
//无法获取到任何表单元素数据(因为form表单的enctype不再是缺省值了,变成了enctype="multipart/form-data")
//String username = request.getParameter("username"); //null
//创建硬盘文件条目工厂对象
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置服务器 “一次” 可以接收的最大数据量(如果文件过大,服务器端需要分多次接收该文件)
factory.setSizeThreshold(4 * 1024);
//设置文件的临时存储目录
String tempPath = this.getServletContext().getRealPath("/tempFile");
//System.out.println(tempPath);//D:\SomethingAboutJava\eGOV\out\artifacts\fileup_war_exploded\tempFile
factory.setRepository(new File(tempPath));
//创建核心对象
ServletFileUpload fileUpload = new ServletFileUpload(factory);
fileUpload.setHeaderEncoding("UTF-8");
//设置可以接收的最大文件
fileUpload.setSizeMax(1024 * 1024 * 1024); //1GB
try {
//核心对象解析请求对象,将请求对象封装成一个一个的FileItem对象,然后存储到一个List集合中
List<FileItem> fileItems = fileUpload.parseRequest(request);
//遍历集合,每次遍历拿出一个FileItem对象
for (FileItem fileItem : fileItems){
if (fileItem.isFormField()){
//说明该表单元素是一个普通的表单元素
String name = fileItem.getFieldName();
//String value = fileItem.getString();
String value = fileItem.getString("UTF-8");
System.out.println(name + "=" + value); //username=zhangsan
}else {
//说明该表单元素是一个文件
String fileName = fileItem.getName();
String path = this.getServletContext().getRealPath("/upFile") + "/" + fileName;
fileItem.write(new File(path));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}