单文件上传和多文件上传

目录

一、单文件上传

二、多文件上传

2.1 多文件选择方式

2.2 多个参数方式


一、单文件上传

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="flie">
    <input type="submit" value="上传">
</form>
</body>
</html>

@RestController
public class uploadController {
    SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");

    @PostMapping("/upload")
    public String upload(MultipartFile flie, HttpServletRequest request) {
        String realPath = request.getServletContext().getRealPath("/");
        String format = sdf.format(new Date());
        String path = realPath + format;
        File folds = new File(path);
        if(!folds.exists()){
            folds.mkdirs();
        }
        String oldName = flie.getOriginalFilename();
        String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));

        try {
            flie.transferTo(new File(folds,newName));
            String s = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + format + newName;
            return s;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

二、多文件上传

2.1 多文件选择方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/uploads1" method="post" enctype="multipart/form-data">
    <input type="file" name="flies" multiple>
    <input type="submit" value="上传">
</form>
</body>
</html>
@PostMapping("/uploads1")
    public List<String> uploads1(MultipartFile flies[], HttpServletRequest request) {
        String realPath = request.getServletContext().getRealPath("/");
        String format = sdf.format(new Date());
        String path = realPath + format;
        File folds = new File(path);
        if(!folds.exists()){
            folds.mkdirs();
        }
        List<String> list = new ArrayList<>();
        try {
        for (MultipartFile flie : flies) {
            String oldName = flie.getOriginalFilename();
            String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
            flie.transferTo(new File(folds,newName));
            String s = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + format + newName;
            list.add(s);
        }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

2.2 多个参数方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/uploads2" method="post" enctype="multipart/form-data">
    <input type="file" name="flie1">
    <input type="file" name="flie2">
    <input type="submit" value="上传">
</form>
</body>
</html>
@PostMapping("/uploads2")
    public List<String> uploads2(MultipartFile flie1,MultipartFile flie2, HttpServletRequest request) {
        List<MultipartFile> flies = new ArrayList<>();
        flies.add(flie1);
        flies.add(flie2);
        String realPath = request.getServletContext().getRealPath("/");
        String format = sdf.format(new Date());
        String path = realPath + format;
        File folds = new File(path);
        if(!folds.exists()){
            folds.mkdirs();
        }
        List<String> list = new ArrayList<>();
        try {
            for (MultipartFile flie : flies) {
                String oldName = flie.getOriginalFilename();
                String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
                flie.transferTo(new File(folds,newName));
                String s = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + format + newName;
                list.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

其实我这里写demo遇到个问题,当我把静态文件放到static下,访问上传的图片是正常的,但是若是我自定义一个文件目录,如实现WebMvcConfigurer接口并定义

registry.addResourceHandler("/**").addResourceLocations("classpath:/dist/");时,出现上传资源访问时被拦截问题,没研究明白,有哪位道友可以说说其中门道呢...

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值