servlet类
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
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.*;
import java.util.List;
import java.util.UUID;
import java.math.BigDecimal;
public class FileServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
if(!ServletFileUpload.isMultipartContent(request)){
return;
}
String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
File uploadFile = new File(uploadPath);
if(!uploadFile.exists()){
uploadFile.mkdir();
}
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
File tempFile = new File(tempPath);
if(!tempFile.exists()){
tempFile.mkdir();
}
try {
DiskFileItemFactory factory = getDiskFileItemFactory(uploadFile);
ServletFileUpload servletFileUpload = getServletFileUpload(factory);
String msg = uploadParseRequest(servletFileUpload, request, uploadPath);
request.setAttribute("msg",msg);
request.getRequestDispatcher("result.jsp").forward(request,response);
} catch (FileUploadException | ServletException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
public static DiskFileItemFactory getDiskFileItemFactory(File file){
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024*1024);
factory.setRepository(file);
return factory;
}
public static ServletFileUpload getServletFileUpload(DiskFileItemFactory factory){
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
servletFileUpload.setProgressListener(new ProgressListener() {
@Override
public void update(long RreadSize, long AllSize, int i) {
BigDecimal bd = new BigDecimal(String.valueOf(RreadSize*100 / AllSize));
bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println("总大小:"+AllSize+"kb\t已上传:"+bd+"%");
}
});
servletFileUpload.setHeaderEncoding("utf8");
servletFileUpload.setFileSizeMax(1024*1024*10);
servletFileUpload.setSizeMax(1024*1024*10);
return servletFileUpload;
}
public static String uploadParseRequest(ServletFileUpload upload,HttpServletRequest request,String uploadPath) throws FileUploadException, IOException {
String msg = "";
List<FileItem> fileItems = upload.parseRequest(request);
for (FileItem fileItem: fileItems){
if(fileItem.isFormField()){
String fieldName = fileItem.getFieldName();
String value = fileItem.getString("utf8");
System.out.println(fieldName + " : " + value);
}else{
String uploadFileName =fileItem.getName();
System.out.println("上传的文件名称:"+uploadFileName);
if(uploadFileName.trim().equals("") || uploadFileName== null){
continue;
}
String fileName = uploadFileName.substring(uploadFileName.lastIndexOf("/")+1);
String fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
System.out.println("文件信息 [文件名称:"+fileName+"\t文件类型:"+fileExtName+"]");
String uuidPath = UUID.randomUUID().toString();
String realPath = uploadPath + "/" + uuidPath;
File realPathFile = new File(realPath);
if(!realPathFile.exists()){
realPathFile.mkdir();
}
InputStream inputStream = fileItem.getInputStream();
FileOutputStream fos = new FileOutputStream(realPath + "/" + fileName);
byte[] buffer = new byte[1024 * 1024];
int len = 0;
while((len=inputStream.read(buffer))>0){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
msg = "文件上传成功";
fileItem.delete();
}
}
return msg;
}
}
index.jsp页面
<body>
<%-- 通过表单上传文件
get:上传文件大小有限制
post:上传文件大小无限制
--%>
<form action="${pageContext.request.contextPath}/upload.do" method="post" enctype="multipart/form-data">
<input type="text" name="username">
<br>
<input type="file" name="file1">
<br>
<input type="submit" value="提交"> | <input type="reset" value="重置">
</form>
</body>
result.jsp页面
<body>
${msg}
</body>