Search in sources :

Example 1 with ImageRes

use of com.ch999.haha.common.ImageRes in project haha by hahafreeasair666.

the class ImgServiceImpl method uploadImg.

@Override
public Imgs uploadImg(MultipartFile file) {
    File file1 = null;
    try {
        if (file.getSize() >= 1024 * 100) {
            // lock.lock();
            log.info("图片压缩");
            String str = IdWorker.get32UUID();
            Thumbnails.of(file.getInputStream()).size(200, 200).toFile(str + ".jpg");
            file1 = new File(str + ".jpg");
            log.info("压缩完毕");
        // lock.unlock();
        }
    } catch (Exception e) {
        log.error("压缩图片异常");
    }
    Imgs imgs = new Imgs();
    String s = HttpClientUtil.uploadFile(IMGSERVER, IMGUPLOAG_URL, file, file1);
    file1.delete();
    log.info("上传完毕,删除压缩图片");
    ImageRes imageRes = JSON.parseObject(s, ImageRes.class);
    String fid = imageRes.getFid();
    String fileName = imageRes.getFileName();
    Integer size = imageRes.getSize();
    if (StringUtils.isBlank(fid) || size == null || size == 0) {
        return null;
    }
    imgs.setImgName(fileName);
    imgs.setImgUrl("http://" + imageRes.getFileUrl().replace("localhost", "120.79.160.214"));
    imgs.setIsdel(0);
    imgsService.insert(imgs);
    return imgs;
}
Also used : Imgs(com.ch999.haha.admin.entity.Imgs) ImageRes(com.ch999.haha.common.ImageRes) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Aggregations

Imgs (com.ch999.haha.admin.entity.Imgs)1 ImageRes (com.ch999.haha.common.ImageRes)1 File (java.io.File)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1