SpringBoot整合Web开发(文件上传)

文件上传

1、单文件上传

  首先创建 Spring Boot 项目并添 spring-boot-starter-web 依赖
然后在 resource 录下的 static 目录中创建一个 upload.html 文件。

1.1、upload.html文件

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

这是一个很简单的文件上传页面,上传接口是upload 注意请求方法是 post , enctype是multipart/form-data

1.2、上传文件代码

@RestController
public class FileUploadController {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest request) {

        String realPath = "D:/IDEA/springboot-vue-test/uploadFile/";
        String format = sdf.format(new Date());
        File file = new File(realPath + format);

        // 判断目录是否存在,不存在则创建目录
        if (!file.isDirectory()) {
            file.mkdirs();
        }

        // 获得上传文件名称
        String oldName = uploadFile.getOriginalFilename();
        // 随机生成文件名称
        String newName = UUID.randomUUID().toString() +
                oldName.substring(oldName.lastIndexOf("."), oldName.length());

        try {
            // 保存文件(路径必须存在)
            uploadFile.transferTo(new File(file, newName));
            // 返回访问url
            String filePath = request.getScheme() + "://" + request.getServerName()
                    + ":" + request.getServerPort() + "/" + format + newName;
            
            return filePath;
        }catch (Exception e){
            e.printStackTrace();
        }

        return "上传失败";
    }
}

最后在浏览器中进行测试。
运行项目,在浏览器中输入http://loclhost:8080/upload.html
注意,所有步骤都是基于项目配置静态资源访问,不然访问不了upload.html,具体怎么配置可以看我主页教程!

2、多文件上传

  首先创建 Spring Boot 项目并添 spring-boot-starter-web 依赖
然后在 resource 录下的 static 目录中创建一个 upload.html 文件。

2.1、uploads.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多文件上传</title>
</head>
<body>
<form action="/uploads" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadFiles" value="请选择文件" multiple />
    <input type="submit" value="上传" />
</form>
</body>
</html>

这是一个很简单的文件上传页面,上传接口是upload 注意请求方法是 post , enctype是multipart/form-data

1.2、上传文件代码

@PostMapping("/uploads")
    public Map uploads(MultipartFile[] uploadFiles, HttpServletRequest request) {

        System.out.println(uploadFiles.length);

        String realPath = "D:/IDEA/springboot-vue-test/uploadFile/";
        String format = sdf.format(new Date());
        Map<String, String> filePaths = new HashMap<>();
        File file = new File(realPath + format);

        // 判断目录是否存在,不存在则创建目录
        if (!file.isDirectory()) {
            file.mkdirs();
        }

        for (int i = 0; i < uploadFiles.length; i++) {

            // 获得上传文件名称
            String oldName = uploadFiles[i].getOriginalFilename();
            // 随机生成文件名称
            String newName = UUID.randomUUID().toString() +
                    oldName.substring(oldName.lastIndexOf("."), oldName.length());

            try {
                // 保存文件(路径必须存在)
                uploadFiles[i].transferTo(new File(file, newName));
                // 返回访问url
                String filePath = request.getScheme() + "://" + request.getServerName()
                        + ":" + request.getServerPort() + "/" + format + newName;
                filePaths.put("上传文件" + (i+1), filePath);
            }catch (Exception e){
                e.printStackTrace();

                filePaths.clear();
                filePaths.put("上传失败", "500");

                return filePaths;
            }
        }

        return filePaths;
    }

最后在浏览器中进行测试。
运行项目,在浏览器中输入http://loclhost:8080/uploads.html
注意,所有步骤都是基于项目配置静态资源访问,不然访问不了uploads.html,具体怎么配置可以看我主页教程!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值