上传图片文件并按指定像素进行压缩

控制层:

@RestController
@RequestMapping("/sharer/file")
@Api(description = "文件上传")
public class FileDataController extends BaseController {

    @Autowired
    OSSUtil ossUtil;

    /**
     * 文件上传具体实现方法;
     * 
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public RetEntity<UploadObject> handleFileUpload(
            @RequestParam("file") MultipartFile file, String path)
            throws IOException {
        RetEntity<UploadObject> ret = new RetEntity<UploadObject>();

        UploadObject u = new UploadObject();
        if (!file.isEmpty()) {
            // 上传原文件
            InputStream input = file.getInputStream();
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 通过文件名获取文件后缀
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            BufferedImage bufImg = ImageIO.read(input);// 把图片读入到内存中
            if (bufImg != null) {
                u = ossUtil.uploadFile(bufImg, suffix, u);

                // 更改图片成功的信息
                ret.setCode(CommonConstant.RESULT_CODE_OK);
                ret.setMessage(MsgConstant.RET_UPLOAD_PICTURE_SUCCESS);
                ret.setObject(u);
                return ret;
            } else {
                ret.setCode(CommonConstant.RESULT_CODE_BAD_REQUEST);
                ret.setMessage(MsgConstant.RET_UPLOAD_NOT_IS_PICTURE);
                return ret;
            }

        } else {
            // 更改图片失败的信息
            ret.setCode(CommonConstant.RESULT_CODE_BAD_REQUEST);
            ret.setMessage(MsgConstant.RET_UPLOAD_PICTURE_FAILURE);
            return ret;
        }
    }
}

工具类:
@Component
public class OSSUtil {

    @Autowired
    AppContext app;
    public UploadObject uploadFile(BufferedImage bufImg, String suffix,
            UploadObject uploadObject) throws IOException {
        //设置初始化的压缩率为1
        float ratio = 1f ;
        //设置压缩后的图片宽度为80
        float width = 80f;
        //获取原图片的宽度、高度
        float ImgWith=bufImg.getWidth();
        float ImgHight=bufImg.getHeight();
        
        // 未压缩
        ByteArrayOutputStream Original_bos = new ByteArrayOutputStream();// 存储图片文件byte数组
        ImageIO.write(bufImg, suffix, Original_bos); // 图片写入到 ImageOutputStream
        
        // 压缩文件
        ByteArrayOutputStream Compress_bos = null;// 存储图片文件byte数组
        
        //根据原始图片宽度,与压缩图宽度重新计算压缩率
        if (ImgWith>width) {
            ratio=width/ImgWith;
        }
        Compress_bos = new ByteArrayOutputStream();
        //按压缩率进行压缩
        bufImg = Thumbnails.of(bufImg).scale(ratio).asBufferedImage();
        ImageIO.write(bufImg, suffix, Compress_bos); // 图片写入到 ImageOutputStream    

        // endpoint以杭州为例,其它region请按实际情况填写
        String endpoint = app.getEndpoint();
        String accessKeyId = app.getAccessKeyId();
        String accessKeySecret = app.getAccessKeySecret();
        String key = IdWorker.get32UUID();
        String Original_key = key + "orig."+suffix;
        String Compress_key = key + "comp."+suffix;

        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

     ossClient.putObject(app.getBucket(), Original_key,
                new ByteArrayInputStream(Original_bos.toByteArray()));
        ossClient.putObject(app.getBucket(), Compress_key,
                new ByteArrayInputStream(Compress_bos.toByteArray()));
        // 原文件的路径 path
        // 服务器原图片的地址 Original_targetPath
        // 服务器压缩后的图片的地址Compress_targetPath
        String Original_targetPath = app.getImageUrl() + "/" + Original_key;
        String Compress_targetPath = app.getImageUrl() + "/" + Compress_key;

        uploadObject.setUrl(Original_targetPath);
        uploadObject.setCompressUrl(Compress_targetPath);
        ossClient.shutdown();
        return uploadObject;
    }
    
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值