SpringBoot实现文件上传配置访问路径映射

1.配置yml
file:
  # 文件路径,使用jvm系统变量,兼容windows和linux;
  # profile: /img
  profile: E:/img

2.配置访问路径
@Configuration
public class UploadFilePathConfig  implements WebMvcConfigurer {

    @Value("${ruoyi.profile}")
    public String uploadFolder;

    /**
     * 文件访问路径映射
     * @param registry
     * addResourceHandler 静态资源对外暴露的访问路径
     * addResourceLocations 内部路径
     * @throws
     * @return void
     **/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/image/**").addResourceLocations("file:" + uploadFolder + "/");
    }
}

3.实现文件上传并返回访问路径
@Component
public class UploadImg {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    public static String uploadFolder;

    @Value("${ruoyi.profile}")
    private void uploadProfile(String profile) {
        uploadFolder = profile;
    }

    /**
     * 文件上传
     * @param file
     * @throws
     * @return java.util.List<java.lang.String>
     **/
    public static List<String> fileUploadImg(MultipartFile file) {
        String customPath = sdf.format(new Date());
        List<String> imgList = new ArrayList<String>();
        if (file != null && !file.isEmpty()) {
            // 获取文件名称
            String fileName = file.getOriginalFilename();
            if (fileName != null && fileName != "") {
                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                // 获取访问网址拼接文件存放路径
                String returnUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/files/img/";
                // 截取文件名后缀
                String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());
                // 重命名
                fileName = System.currentTimeMillis() + "_" + new Random().nextInt(1000) + fileF;
                String fileMulu = uploadFolder + "/" + customPath;
                Establish(fileMulu);
                File file1 = new File(fileMulu);
                // 保存文件
                File targetFile = new File(file1, fileName);
                try {
                    byte[] buffer = new byte[1024 * 1024];
                    InputStream is = file.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    OutputStream os = new FileOutputStream(targetFile);
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    int bytesRead = 0;
                    while ((bytesRead = bis.read(buffer, 0, 1024 * 1024)) != -1) {
                        bos.write(buffer, 0, bytesRead);
                        bos.flush();
                    }
                    bos.close();
                    bis.close();
                    os.close();
                    is.close();
                    String msg = returnUrl + customPath + "/" + fileName;
                    imgList.add(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return imgList;
    }

    public static void Establish(String fileMulu) {
        File file = new File(fileMulu);
        if (!file.exists()){
            file.mkdirs();
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值