<%--表单上传文件
get:上传文件大小有限制
post:没有限制
--%>
<form action="" enctype="multipart/form-data" method="post">
上传用户:<input type="text" name="username"><br>
<p><input type="file"/></p>
<p><input type="file"/></p>
<p><input type="submit">|<input type="reset"> </p>
</form>
public class UploadFileServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = "";
if (!ServletFileUpload.isMultipartContent(req)) {
return;
}
try {
String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdir();
}
String tmpPath = this.getServletContext().getRealPath("/WEB-INF/temp");
File tmpfile = new File(tmpPath);
if (!tmpfile.exists()) {
tmpfile.mkdir();
}
DiskFileItemFactory factory = getDiskFileItemfactory(tmpfile);
ServletFileUpload upload = getServletFileUpload(factory);
msg = uploadParseRequest(upload, req, uploadPath);
} catch (Exception e) {
e.printStackTrace();
}
req.setAttribute("msg", msg);
req.getRequestDispatcher("info.jsp").forward(req, resp);
}
private DiskFileItemFactory getDiskFileItemfactory(File tmpfile) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024);
factory.setRepository(tmpfile);
return factory;
}
private ServletFileUpload getServletFileUpload(DiskFileItemFactory factory) {
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setProgressListener(new ProgressListener() {
@Override
public void update(long pBytesRead, long pContentLength, int PItems) {
System.out.println("总大小:" + pContentLength + "已上传:" + pBytesRead);
}
});
upload.setHeaderEncoding("UTF-8");
upload.setFileSizeMax(1024 * 1024 * 10);
upload.setSizeMax(1024 * 1024 * 10);
return upload;
}
private String uploadParseRequest(ServletFileUpload upload, HttpServletRequest req, String uploadPath) throws IOException {
List<FileItem> fileItems = null;
String msg = "";
try {
fileItems = upload.parseRequest(req);
} catch (FileUploadException e) {
e.printStackTrace();
}
for (FileItem fileItem : fileItems) {
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();
String value = fileItem.getString("utf-8");
String uploadFileName = fileItem.getName();
if (uploadFileName.trim().equals("") || uploadFileName == null) {
continue;
}
String fileName = uploadFileName.substring(uploadFileName.lastIndexOf("/") + 1);
String fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
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;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}