File-java实现文件上传功能

一、文件上传准备工作

对于文件上传,浏览器在上传的过程中将文件以流的形式提交到服务器。
可以选择apache的commons-fileupload包作为文件上传组件,commons-fileupload包依赖于commons-io包。
可以在Maven导入该commons-fileupload包,Maven会帮我们导入依赖的jar包commons-io。

<dependency>
 <groupId>commons-fileupload</groupId>
 <artifactId>commons-fileupload</artifactId>
 <version>1.3.3</version>
</dependency>

二、文件上传的主要步骤

创建diskFileItemFactory对象,处理文件上传路径或者大小限制

通过diskFileItemFactory对象作为ServletFileUpload类的参数,创建ServletFileUpload对象

处理上传的文件

三、代码实现

在文件上传时,表单一定要加enctype=“multipart/form-data” 。只有使用enctype=“multipart/form-data”,表单才会把文件的内容编码到HTML请求中。
默认enctype=“application/x-www-form-urlencoded”,表单的内容会按URL规则编码。
而enctype="multipart/form-data"不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。method也一定要使用post请求。因为get请求会有大小的限制。

<form action="/file.do" enctype="multipart/form-data" method="post">
 <p>上传用户:<input type="text" name="username"></p>
 <p><input type="file" name="file1"></p>
 <p><input type="file" name="file2"></p>
 <p><input type="submit">|<input type="reset"></p>
</form>

定义一个类去继承HttpServlet类在doPost方法里面实现功能

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 //判断上传的是普通表单还是带文件的表单
 if (!ServletFileUpload.isMultipartContent(req)) {
  return;
 }
 //创建上传文件保存的地址,一般创建在WEB-INF目录下,用户无法直接访问该目录下的文件
 String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
 File uploadFile = new File(uploadPath);
 //如果文件夹不存在,则创建一个
 if (!uploadFile.exists()) {
  uploadFile.mkdir();
 }
 
 //创建上传大文件的临时地址,一般几天后自动删除,用户可以手动删除或者转为永久文件
 // 一般创建在WEB-INF目录下,用户无法直接访问该目录下的文件
 String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
 File tempFile = new File(tempPath);
 //如果文件夹不存在,则创建一个
 if (!tempFile.exists()) {
  tempFile.mkdir();
 }
 
 //1.创建diskFileItemFactory对象,处理文件上传路径或者大小限制
 DiskFileItemFactory factory = getDiskFileItemFactory(tempFile);
 
 //2.获取ServletFileUpload
 ServletFileUpload upload = getServletFileUpload(factory);
 
 //3.处理上传的文件
 try {
  String msg = uploadParseRequest(upload, req, uploadPath);
  //转发
  req.setAttribute("msg",msg);
  req.getRequestDispatcher("info.jsp").forward(req, resp);
 } catch (FileUploadException e) {
  e.printStackTrace();
 }
 }
 
 
 public DiskFileItemFactory getDiskFileItemFactory(File file) {
 DiskFileItemFactory factory = new DiskFileItemFactory();
 //创建一个缓存区,当上传文件大于设置的缓存区时,将该文件放到临时目录
 factory.setSizeThreshold(1024 * 1024);//缓存区大小为1M
 factory.setRepository(file);//临时目录
 return factory;
 }
 
 public ServletFileUpload getServletFileUpload(DiskFileItemFactory factory) {
 ServletFileUpload upload = new ServletFileUpload(factory);
 //监听文件上传进度
 upload.setProgressListener(new ProgressListener() {
  @Override
  public void update(long uploaded, long totalSize, int i) {
  System.out.println("已上传:"+(uploaded*100)/totalSize+"%");
  }
 });
 upload.setHeaderEncoding("UTF-8");//乱码处理
 upload.setFileSizeMax(1024 * 1024 * 10);//设置单个文件的最大值10M
 upload.setSizeMax(1024 * 1024 * 100);//设置总共能上传文件的最大值100M
 return upload;
 }
 
 public String uploadParseRequest(ServletFileUpload upload, HttpServletRequest req, String uploadPath) throws FileUploadException, IOException {
 String msg = "";
 //把前端请求解析,封装成一个List对象
 List<FileItem> fileItems = upload.parseRequest(req);
 for (FileItem fileItem : fileItems) {
  if (fileItem.isFormField()) {//判断上传的文件是普通的表单还是带文件的表单
  String name = fileItem.getName();//前端表单控件的name:username
  String value = fileItem.getString("UTF-8");//乱码处理
  System.out.println(name + ":" + value);
  } else {//判断为上传的文件
  //==================处理文件=====================
  String uploadFileName = fileItem.getName();//前端表单控件的name
  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 + "--文件后缀:" + fileExtName);
  //==================存放地址==================
  String uuidPath = UUID.randomUUID().toString();
  //文件存储的真实路径
  String realPath = uploadPath + "/" + uuidPath;
  System.out.println("文件上传到的位置:"+realPath);
  //给每个文件创建一个文件夹
  File realPathFile = new File(realPath);
  if (!realPathFile.exists()) {//如果文件夹不存在,则创建一个
   realPathFile.mkdir();
  }
  //==================文件传输==================
  //获得文件上传的流
  InputStream inputStream = fileItem.getInputStream();
  //创建一个文件输出流
  FileOutputStream fileOutputStream = new FileOutputStream(realPath + "/" + fileName);
  //创建一个缓冲区
  byte[] buffer = new byte[1024 * 1024];
  //判断读取是否完毕
  int len = 0;
  while ((len = inputStream.read(buffer)) > 0) {
   fileOutputStream.write(buffer, 0, len);
  }
  //关闭流
  fileOutputStream.close();
  inputStream.close();
  msg = "上传成功";
  fileItem.delete();//上传成功,清除临时文件
  }
 }
 return msg;
}

文件上传的注意事项

1、为保证服务器安全,上传的文件应该放在外界无法直接访问的目录下,例如放在WEB-INF目录下。
2、为了防止文件覆盖的现象,要为上传的文件产生一个唯一的文件名(可以为每一个上传的文件增加一个uuid或时间戳为名的文件夹)
3、限制上传文件的最大值
4、限制上传文件的类型,在收到上传文件时,判断其后缀名是否合法,是否为自己限制的类型
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值