加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

SpringMVC 单文件,多文件上传实现详解

发布时间:2020-12-14 19:39:59 所属栏目:Java 来源:网络整理
导读:需要用到的流的相关知识:https://www.jb51.net/article/170640.htm SpringMVC中写好了文件上传的类。 要使用文件上传,首先需要文件上传相关的Jar包。commons-fileupload.jar 和 commons-io.jar。 添加到pom.xml或lib文件夹下。 pom.xml: dependency group

需要用到的流的相关知识:https://www.aspzz.cn/article/170640.htm

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

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

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

pom.xml:

<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

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

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

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

<form action="handler/testUpload" method="post" enctype="multipart/form-data">
  文件描述: <input type="text" name="desc" id="desc">
  <br>
  请选择文件: <input type="file" name="file"><br>
  <input type="submit" value="上传文件">
</form>
<br>
<br>
<form action="handler/testMutiUpload" method="post" enctype="multipart/form-data">
  文件描述: <input type="text" name="desc">
  <br>
  请选择文件: <input type="file" name="file"><br>
  请选择文件1: <input type="file" name="file"><br>
  请选择文件2: <input type="file" name="file"><br>
  <input type="submit" value="上传文件">
</form>

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

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

单文件上传

也可以不使用流,下面这句看到有人使用,但是没有测试。

File dest = new File(filePath + fileName); file.transferTo(dest);

@RequestMapping("testUpload")
  public String testUpload(@RequestParam("desc") String desc,@RequestParam("file") MultipartFile file) throws IOException {
    System.out.println("文件描述:" + desc);
    // 得到文件的输入流
    InputStream inputStream = file.getInputStream();
    // 得到文件的完整名字 img.png/hh.docx
    String fileName = file.getOriginalFilename();
    // 输出流
    OutputStream outputStream = new FileOutputStream("C:tmp" + fileName);
    // 缓冲区
    byte[] bs = new byte[1024];
    int len = -1;
    while ((len = inputStream.read(bs)) != -1) {
      outputStream.write(bs,len);
    }
    inputStream.close();
    outputStream.close();
    return "success";
  }

多文件上传

@RequestMapping("testMutiUpload")
  public String testMutiUpload(@RequestParam("desc") String desc,@RequestParam("file") MultipartFile[] files) throws IOException {
    System.out.println("文件描述:" + desc);
    for (MultipartFile file :
        files) {
      InputStream inputStream = file.getInputStream();
      String fileName = file.getOriginalFilename();
      OutputStream outputStream = new FileOutputStream("C:tmp" + fileName);
      byte[] bs = new byte[1024];
      int len = -1;
      while ((len = inputStream.read(bs)) != -1) {
        outputStream.write(bs,len);
      }
      inputStream.close();
      outputStream.close();
    }
    return "success";
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读