SpringBoot实现多文件上传的详细示例代码

文件上传

Spring Boot代码

1.代码结构:

SpringBoot实现多文件上传的详细示例代码_第1张图片

2.Controller层

package com.yqifei.upload.controller;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName UploadController
 * @Description TODO
 * @Author jiangyuntao
 * @Data 2023/3/7 23:52
 * @Version 1.0
 * @Email [email protected]
 */

@RestController
@CrossOrigin
@RequestMapping("/posts")
@Api(tags = "文件上传控制器")
public class UploadController {

/*
http://localhost:8088/swagger-ui.html#
*/
    @PostMapping(value="/upload")
    @CrossOrigin
    public List fileload(@RequestParam(value = "file") MultipartFile[] file, HttpServletRequest request) throws IOException {
        System.out.println(file.length);
        String savaLaction="d:/data/";
        String fileSaveName;
        List imageUri = new ArrayList<>();
        for (MultipartFile multipartFile:file) {
            System.out.println("文件"+multipartFile.getOriginalFilename());
            fileSaveName = UUID.randomUUID().toString()+multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File(savaLaction,fileSaveName));
            String res = request.getScheme()+"://"+request.getServerName()+":"+"8080"+savaLaction+"/"+fileSaveName;
            imageUri.add(res);
        }
        System.out.println(imageUri);
        return imageUri;
    }

}

3.跨域拦截器配置

package com.yqifei.upload.utils;

import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "CorsFilter")
@Configuration
public class CorsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
    }
}

4.application.properties配置

# 应用名称
spring.application.name=upload
# 应用服务 WEB 访问端口
server.port=8088

spring.web.resources.static-locations=file:d:/data/

spring.servlet.multipart.max-request-size=50MB
spring.servlet.multipart.max-file-size=50MB

5.前端页面



  
    
    Multiple File Upload
  
  
    

Multiple File Upload

图片返回值地址:

6.效果展示

SpringBoot实现多文件上传的详细示例代码_第2张图片

7.获取图片的url并且读取图片

SpringBoot实现多文件上传的详细示例代码_第3张图片

修改tomcat的server.xml文件

SpringBoot实现多文件上传的详细示例代码_第4张图片

加上下面这句


// 	docBase代表文件路径,path是浏览器访问时的路径。
// 若自己创建的文件夹在tomcat目录的webapps中,不同之处: docBase直接写文件夹文字即可(注意:没有/) 例如 docBase ="home/springbootVue/files"

总结

到此这篇关于SpringBoot实现多文件上传的文章就介绍到这了,更多相关SpringBoot多文件上传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(SpringBoot实现多文件上传的详细示例代码)