资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springboot中怎么实现多文件上传功能-创新互联

springboot中怎么实现多文件上传功能,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联建站长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为阳泉企业提供专业的成都网站制作、网站设计、外贸网站建设阳泉网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

首先创建一个springboot项目,添加spring-boot-starter-web依赖。

然后在resources下的static文件夹下创建uploads.html文件,文件内容如下:

   多文件上传

   

然后编写Controller类

@RestControllerpublic class FilesUploadController {   SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");   @RequestMapping("/uploads")  public String upload(MultipartFile[] uploadFiles, HttpServletRequest request) {    List list = new ArrayList();//存储生成的访问路径    if (uploadFiles.length > 0) {      for (int i = 0; i < uploadFiles.length; i++) {        MultipartFile uploadFile = uploadFiles[i];        //设置上传文件的位置在该项目目录下的uploadFile文件夹下,并根据上传的文件日期,进行分类保存        String realPath = request.getSession().getServletContext().getRealPath("uploadFile");        String format = sdf.format(new Date());        File folder = new File(realPath + format);        if (!folder.isDirectory()) {          folder.mkdirs();        }         String oldName = uploadFile.getOriginalFilename();        System.out.println("oldName = " + oldName);        String newName = UUID.randomUUID().toString() + oldName.            substring(oldName.lastIndexOf("."), oldName.length());        System.out.println("newName = " + newName);        try {          //保存文件          uploadFile.transferTo(new File(folder, newName));           //生成上传文件的访问路径          String filePath = request.getScheme() + "://" + request.getServerName() + ":"+ request.getServerPort() + "/uploadFile" + format + newName;          list.add(filePath);        } catch (IOException e) {          e.printStackTrace();        }      }      return list.toString();    } else if (uploadFiles.length == 0) {      return "请选择文件";    }    return "上传失败";  }}

相比于单文件上传,这里就多了一个遍历的过程。

文件上传常见配置:

#是否开启文件上传支持,默认是truespring.servlet.multipart.enabled=true #文件写入磁盘的阈值,默认是0spring.servlet.multipart.file-size-threshold=0#上传文件的临时保存位置spring.servlet.multipart.location=D:\\upload#单个文件的较大值,默认是1MBspring.servlet.multipart.max-file-size=1MB#多个文件上传时的总大小 值,默认是10MBspring.servlet.multipart.max-request-size=10MB#是否延迟解析,默认是falsespring.servlet.multipart.resolve-lazily=false

关于springboot中怎么实现多文件上传功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


新闻标题:springboot中怎么实现多文件上传功能-创新互联
标题链接:http://cdkjz.cn/article/cechis.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220