摘自:https://www.cnblogs.com/896240130Master/p/6430908.html

   https://blog.csdn.net/kouwoo/article/details/40507565

注意:单文件MultipartFile file

   多文件(定义为数组形式)MultipartFile[] file

判断文件是否为空:!file.isEmpty() -- 不为空

文件保存路径 :String filePath = request.getSession().getServletContext().getRealPath("/") 

               + "upload/" + file.getOriginalFilename();

上传文件原名:file.getOriginalFilename();

转存文件 :file.transferTo(new File(filePath));

步骤:1、创建一个控制类

   2、编写提交表单的action

   3、使用SpringMVC注解RequestParam来指定表单中的file参数;

   4、指定一个用于保存文件的web项目路径

   5、通过MultipartFile的transferTo(File dest)这个方法来转存文件到指定的路径。

  MultipartResolver其中属性详解:

    defaultEncoding="UTF-8" 是请求的编码格式,默认为iso-8859-1
    maxUploadSize="5400000" 是上传文件的大小,单位为字节
    uploadTempDir="fileUpload/temp" 为上传文件的临时路径

 1 <body>  
 2 <h2>文件上传实例</h2>  
 3   
 4   
 5 <form action="fileUpload.html" method="post" enctype="multipart/form-data">  
 6     选择文件:<input type="file" name="file">  
 7     <input type="submit" value="提交">   
 8 </form>  
 9   
10   
11 </body>  
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-18
  • 2021-09-29
  • 2021-10-19
  • 2021-06-30
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2021-08-03
  • 2021-09-12
  • 2021-08-01
相关资源
相似解决方案