图片上传,网络图片路径配置

图片上传代码

 @value("${tupian.url}")
 private String url;
 private static final String SEPARATOR = "/";
 @Autowired
 private StaticImagePath staticImagePath;
 @Override
    public ResultBean fileUpload(MultipartFile file,Integer type) {
        String uuid = UUIDUtils.getUUID();
        String fileName = file.getOriginalFilename();
        log.info("上传的文件名{}==>"+fileName);
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String today = DateUtils.getCurrentDate("yyyy-MM-dd");
        String path = staticImagePath.getUploadPath();
        //存储文件到磁盘
        StringBuilder dictPath = new StringBuilder();
        String filetType ="";
        if (0 == type){
            filetType = "image";
        }

        if (1 == type){
            filetType = "video";
        }
        dictPath.append(path).append(today).append(SEPARATOR).append(filetType).append(SEPARATOR).append(uuid).append(suffix);
        File dictFile = new File(dictPath.toString());
        if(!dictFile.getParentFile().exists()) {
            dictFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dictFile);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultBean(500,"文件上传失败");
        }
        JSONObject jsonObject = new JSONObject();
        String src = url+today+SEPARATOR+filetType+SEPARATOR+uuid+suffix;
        jsonObject.put("src",src);
        return new ResultBean(0,"文件上传成功",jsonObject);
    }

#网络图片配置

tupian:
  uploadPath: d:/img/
  url: http://ip:${server.port}/files/


@Data
@Component
@ConfigurationProperties(prefix = "tupian")
public class StaticImagePath {

    private String uploadPath;

    private String url;
}
 @Configuration
 public class WebConfigur implements WebMvcConfigurer {
 @Autowired
 private StaticImagePath staticImagePath;
 @Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/files/**")
                .addResourceLocations("file:///"+staticImagePath.getUploadPath());
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

会跑的葫芦怪

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值