单文件和多文件上传

SpringBoot单文件和多文件上传

先创建一个UploadFileService接口,封装好单文件上传的文件接收方法,多文件上传使用循环调用接口即可

单文件上传

一、首先:UploadFileService接口

package edu.zhku.fire_ant_project.service;

import edu.zhku.fire_ant_project.comment.Result;
import edu.zhku.fire_ant_project.entity.Slideshow;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class UploadFileService {
    @Autowired
    private HttpServletRequest req;


    public String saveUploadFile(MultipartFile uploadFile) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

        // 上传的文件将保存在项目运行目录下的 uploadFile 文件夹,
        String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");//工程目录创建
        // 并且在 uploadFile 文件夹中通过日期对上传的文件归类保存
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        // 对上传的文件重命名,避免文件重名
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString()
                + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            // 文件保存
            uploadFile.transferTo(new File(folder, newName));

            // 返回上传文件的访问路径
            String filePath = req.getScheme() + "://" + req.getServerName()
                    + ":" + req.getServerPort() + "/fire_ant_project/uploadFile/" + format + newName;

            return filePath;

        } catch (IOException e) {
            e.printStackTrace();

        }

        return null;
    }
}


二、单文件上传
直接调用即可:


    @PostMapping("/uploadFile")
    public Map<String,Object> uploadFile(@RequestParam("file") MultipartFile uploadFile){
            Map map=new HashMap<>();
            try {
                String filepath = uploadFileService.saveUploadFile(uploadFile);
                map.put("filepath",filepath);
            } catch (NullPointerException e) {
                map.put("filepath","没有找到文件");
            }
            return map;
        }

多文件上传

直接调用循环函数即可

 @PostMapping("/uploadFile")
    public Map<String,Object> uploadFile(@RequestParam("file") MultipartFile[] uploadFile){
        Map map=new HashMap<>();
        try {
            if (uploadFile.length > 0) {
                for (int i = 0; i < uploadFile.length; i++) {
                    String filepath = uploadFileService.saveUploadFile(uploadFile[i]);
                    map.put("filepath", filepath);
                }
            }else {
                map.put("filepath","没有找到文件");
                }

        } catch (NullPointerException e) {
            map.put("filepath","没有找到文件");
        }
        System.out.println(map);
        return map;
    }

文件上传页面:

在这里插入图片描述

action:项目的相对路径
单文件上传:去掉multiple
多文件上传:加上multiple

运行结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锐行织梦者

谢谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值