SpringBoot图片上传并将url存储在数据库

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

技术栈:

SpringBoot+mybatis+mysql+freemarker

      之前将图片存储在项目的路径中,导致每次上传后都需要重新编译才能读出图片,查阅相关资料后发现,图片需要单独的路径进行存储,并对路径进行配置。

controller:

@RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
@ResponseBody
public Map uploadImg(@RequestParam(value = "file",required = false) MultipartFile file){
     return productServiceImp.uploadImg(file);
}

service:

@Value("${absoluteImgPath}")
String absoluteImgPath;

@Value("${sonImgPath}")
String sonImgPath;

@Value("${server.port}")
String port;

@Value("${host}")
String host;

@Override
public Map uploadImg(MultipartFile file) {
        Map map = new HashMap<>();
        if(file.isEmpty()){
            map.put("code","500");
            map.put("msg","图片不能为空");
            return map;
        }
        String originalFilename = file.getOriginalFilename();
        //随机生成文件名
        String fileName = RandomUtils.createRandomString(10) + originalFilename;
        File dest = new File(absoluteImgPath + fileName);
        String imgUrl = sonImgPath +fileName;
        try{
            file.transferTo(dest);
            map.put("code","200");
            map.put("msg","上传成功");
            map.put("imgUrl",imgUrl);
            return map;
        }catch (IllegalStateException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return map;
}

随机数生成:

public class RandomUtils {

    private static final String charlist = "0123456789";

    public static String createRandomString(int len) {
        String str = new String();
        for (int i = 0; i < len; i++) {
            str += charlist.charAt(getRandom(charlist.length()));
        }
        return str;
    }

    public static int getRandom(int mod) {
        if (mod < 1) {
            return 0;
        }
        int ret = getInt() % mod;
        return ret;
    }

    private static int getInt() {
        int ret = Math.abs(Long.valueOf(getRandomNumString()).intValue());
        return ret;
    }

    private static String getRandomNumString() {
        double d = Math.random();
        String dStr = String.valueOf(d).replaceAll("[^\\d]", "");
        if (dStr.length() > 1) {
            dStr = dStr.substring(0, dStr.length() - 1);
        }
        return dStr;
    }
}

application.properties:

absoluteImgPath = C://Users/Administrator/Desktop/ECS/images/
sonImgPath = /images/

路径配置:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${absoluteImgPath}")
    String absoluteImgPath;

    @Value("${sonImgPath}")
    String sonImgPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/web/images/");
        registry.addResourceHandler(sonImgPath + "**").addResourceLocations("file:"+absoluteImgPath);
    }
}

数据库存储的url形式:

/images/9045969040S90.jpg

 

转载于:https://my.oschina.net/u/3424784/blog/3009669

你可能感兴趣的:(SpringBoot图片上传并将url存储在数据库)