上传文件
1、上传文件
没必要记住,直到流程就行了,用的时候直接copy过来用,嘿嘿
/**
* @author zhangbingbing
* @version 1.0
* @date 2020/5/13 12:58
*/
public class FileServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入了我。。。。。");
//1、判断是否为包含文件上传的表单
if (!ServletFileUpload.isMultipartContent(request)) {
return; //如果不是直接结束
}
//2、创建上传的保存路径,建议保存在WEB-INF目录下,用户无法直接上传
String upload = this.getServletContext().getRealPath("/WEB-INF/upload");
File uploadFile = new File(upload);
if (!uploadFile.exists()) {
uploadFile.mkdir();
}
//3、创建大文件的缓存路径,为什么?因为服务器存储空间不是无限的,让大文件几天后自动删除
String temp = this.getServletContext().getRealPath("/WEB-INF/tem");
File tempFile = new File(upload);
if (!tempFile.exists()) {
tempFile.mkdir(); //没有我们就创建
}
//4、创建DiskFileItemFactory对象来处理文件上传路径或者大小限制
try {
DiskFileItemFactory factory = getDiskFileItemFactory(tempFile);
//5、用DiskFileItemFactory对象创建文件上传对象:ServletFileUpload对象
ServletFileUpload fileUpload = getServletFileUpload(factory);
//6、处理上传的文件
String msg = uploadParseRequest(request, fileUpload, uploadFile);
System.out.println(msg);
request.getRequestDispatcher("success.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
//4、获取操作磁盘对象的对象
private static DiskFileItemFactory getDiskFileItemFactory(File tempFile) {
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置传输速度阈值
factory.setSizeThreshold(1024*1024);
factory.setRepository(tempFile);
return factory;
}
//5、获取文件传输对象
private static ServletFileUpload getServletFileUpload(DiskFileItemFactory factory) {
ServletFileUpload fileUpload = new ServletFileUpload(factory);
fileUpload.setHeaderEncoding("utf-8");
fileUpload.setFileSizeMax(1024*1024*10);
fileUpload.setSizeMax(1024*1024*10);
return fileUpload;
}
//6、处理上传的文件,因为有的可能不是文件
private static String uploadParseRequest(HttpServletRequest request, ServletFileUpload upload, File path) throws Exception {
String msg = "";
List<FileItem> fileItems = upload.parseRequest(request);
for (FileItem fileItem : fileItems) {
//判断是否是上传的文件
if (fileItem.isFormField()) { //不是上传的文件
String fieldName = fileItem.getFieldName();
String string = fileItem.getString("UTF-8");
System.out.println("fieldName:"+string);
} else {
//是文件
//处理文件
String fileName = fileItem.getName();
System.out.println("fileName:"+fileName);
if (fileName == null || fileName.trim().equals("")) {
continue;
}
//是真正要上传的文件
String substring1 = fileName.substring(fileName.lastIndexOf("/") + 1);
String substring2 = fileName.substring(fileName.lastIndexOf(".") + 1);
String UUIDString = UUID.randomUUID().toString();
String realPath = path + "/" + UUIDString;
File realPathFile = new File(realPath);
if (!realPathFile.exists()) {
realPathFile.mkdir();
}
InputStream inputStream = fileItem.getInputStream();
FileOutputStream fos = new FileOutputStream(realPath+"/"+substring1);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
inputStream.close();
fos.close();
msg = "文件上传成功!";
fileItem.delete();
}
}
return msg;
}
流程还是要看一遍的!