【SpringMVC】SpringMVC中文件上传-单文件,多文件上传

本是古典 何须时尚 2021-10-29 11:22 506阅读 0赞

需要用到的流的相关知识:https://www.cnblogs.com/to-red/p/9813281.html

SpringMVC中写好了文件上传的类。

要使用文件上传,首先需要文件上传相关的Jar包。commons-fileupload.jar 和 commons-io.jar。

添加到pom.xml或lib文件夹下。

pom.xml:

  1. <dependency>
  2. <groupId>commons-fileupload</groupId>
  3. <artifactId>commons-fileupload</artifactId>
  4. <version>1.3.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>commons-io</groupId>
  8. <artifactId>commons-io</artifactId>
  9. <version>2.4</version>
  10. </dependency>

在SprigMVC的配置文件中添加bean(id和class都是固定写法):

  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  2. <property name="defaultEncoding" value="UTF-8"></property>
  3. <property name="maxUploadSize" value="104857600"></property>
  4. </bean>

前端写一个单文件上传的表单,一个多文件上传的表单(多文件上传的表单中,多个文件输入input中的name相同):

  1. <form action="handler/testUpload" method="post" enctype="multipart/form-data">
  2. 文件描述: <input type="text" name="desc" id="desc">
  3. <br>
  4. 请选择文件: <input type="file" name="file"><br>
  5. <input type="submit" value="上传文件">
  6. </form>
  7. <br>
  8. <br>
  9. <form action="handler/testMutiUpload" method="post" enctype="multipart/form-data">
  10. 文件描述: <input type="text" name="desc">
  11. <br>
  12. 请选择文件: <input type="file" name="file"><br>
  13. 请选择文件1: <input type="file" name="file"><br>
  14. 请选择文件2: <input type="file" name="file"><br>
  15. <input type="submit" value="上传文件">
  16. </form>

文件上传中,参数要使用MultipartFile而不是File类,不能使用FileUtils.copyFile()来复制文件,因此使用流来输出到磁盘

单文件多文件只是将单文件中传递来的file参数改为数组形式,将方法内的file有关的操作都变为数组即可。

  • 单文件上传

    @RequestMapping(“testUpload”)

    1. public String testUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException {
    2. System.out.println("文件描述:" + desc);
    3. // 得到文件的输入流
    4. InputStream inputStream = file.getInputStream();
    5. // 得到文件的完整名字 img.png/hh.docx
    6. String fileName = file.getOriginalFilename();
    7. // 输出流
    8. OutputStream outputStream = new FileOutputStream("C:\\tmp\\" + fileName);
    9. // 缓冲区
    10. byte[] bs = new byte[1024];
    11. int len = -1;
    12. while ((len = inputStream.read(bs)) != -1) {
    13. outputStream.write(bs,0,len);
    14. }
    15. inputStream.close();
    16. outputStream.close();
    17. return "success";
    18. }
  • 多文件上传

    @RequestMapping(“testMutiUpload”)

    1. public String testMutiUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile[] files) throws IOException {
    2. System.out.println("文件描述:" + desc);
    3. for (MultipartFile file :
    4. files) {
    5. InputStream inputStream = file.getInputStream();
    6. String fileName = file.getOriginalFilename();
    7. OutputStream outputStream = new FileOutputStream("C:\\tmp\\" + fileName);
    8. byte[] bs = new byte[1024];
    9. int len = -1;
    10. while ((len = inputStream.read(bs)) != -1) {
    11. outputStream.write(bs,0,len);
    12. }
    13. inputStream.close();
    14. outputStream.close();
    15. }
    16. return "success";
    17. }

转载于:https://www.cnblogs.com/to-red/p/11349108.html

发表评论

表情:
评论列表 (有 0 条评论,506人围观)

还没有评论,来说两句吧...

相关阅读

    相关 文件

    现象: 实现多文件上传: ![这里写图片描述][SouthEast] 解决办法: 1:在页面提交表单加上如下代表表单中有文件上传 ![这里写图片描述][Sou