压缩并上传图片到阿里云(jfinal)

/**
* 获取上传文件
*
* @param r
* @Return: com.oreilly.servlet.multipart.FilePart
*/
public static FilePart fileuploads(HttpServletRequest r) {
boolean flag = false;
MultipartParser mp;
try {
mp = new MultipartParser(r, 52428800, false, false, "UTF-8");
Part part = null;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isFile()) {
FilePart filePart = (FilePart) part;
return filePart;
}
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}

/**
* 判断图片类型
*
* @param ImgType
* @Return: java.lang.String
*/
public static String judgeImgType(String ImgType) {
Map<String, String> imgMap = new HashMap<>();
imgMap.put("images", "gif,jpg,jpeg,png,bmp");
String imgType = "";
if (StringUtils.isNotBlank(ImgType)) {
if (Arrays.<String>asList(imgMap.get("imgaes").split(",")).contains(ImgType)) {
imgType = ImgType;
} else {
imgType = "false:文件格式错误";
}
}
return imgType;
}

/**
* 压缩图片(没有原图)
* 默认输出25%质量图片
*
* @param image
* @return
* @throws IOException
*/
public static InputStream compress(BufferedImage image, String readImageFormat) throws IOException {
InputStream in = ImageUtils.getInputStream(image, readImageFormat);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Thumbnails.of(in).size(image.getWidth(), image.getHeight()).outputQuality(0.25f).toOutputStream(os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
in.close();
os.close();
return is;
}

#阿里云oos
endPoint = oss-cn-beijing.aliyuncs.com
accessKeyId = LTAI9rV1x0TmtGjq
accessKeySecret = 3QCJw4MhlyZC8zQUATKaLxWZpk4bFY
bucketName = songsiraliyun
urlKey = images/
fileUrlKey = files/
public static String REQHEAD = PropKit.get("reqHead");
public static String ENDPOINT = PropKit.get("endPoint");
public static String ACCESSKEYID = PropKit.get("accessKeyId");
public static String ACCESSKEYSECRET = PropKit.get("accessKeySecret");
public static String BUCKETNAME = PropKit.get("bucketName");
public static String URLKEY = PropKit.get("urlKey");
public static String FILEURLKEY = PropKit.get("fileUrlKey");
/**
* 上传压缩图片到阿里云
*
* @Return: void
*/
public void uploadByAliYun() throws IOException {
FilePart fileUploads = HttpUtil.fileuploads(getRequest());
String trimFileName = fileUploads.getFileName().trim();
String subName = trimFileName.substring(0, trimFileName.lastIndexOf("."));
String fileType = trimFileName.substring(trimFileName.lastIndexOf("."));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String realName = "";
//修改文件名,如果相同就会被覆盖掉
realName = simpleDateFormat.format(new Date()) + "-" + RandCharsUtils.getRandomString(16) + fileType;
BufferedImage bufferedImage = ImageIO.read(fileUploads.getInputStream());
    判断文件大小
if (bufferedImage.getWidth() * bufferedImage.getHeight() <= 104857600) {
String fileExt = fileUploads.getFileName().substring(fileUploads.getFileName().lastIndexOf(".") + 1).toLowerCase();
String judgeImgType = ImageUtils.judgeImgType(fileExt);
      //判断文件类型
if (StringUtils.isNotBlank(judgeImgType) && !StringUtils.contains(judgeImgType, "false")) {
        //开始压缩图片
InputStream compress = ImageUtils.compress(bufferedImage, judgeImgType);
        //上传到阿里云
OSSClient ossClient = new OSSClient(REQHEAD + ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
ossClient.putObject(BUCKETNAME, URLKEY + realName, compress);
ossClient.shutdown();
Record record = new Record();
record.set("url", REQHEAD + BUCKETNAME + '.' + ENDPOINT + '/' + URLKEY + realName);
renderJson(setResult(record));
} else {
Map<String, String> resMap = new HashMap<>(16);
resMap.put("code", "5000");
resMap.put("msg", "上传图片失败");
renderJson(setResult(resMap));
}
} else {
Map<String, String> resMap = new HashMap<>(16);
resMap.put("code", "5000");
resMap.put("msg", "上传" + subName + "图片过大");
renderJson(setResult(resMap));
}
}

参考文档:
https://blog.csdn.net/distinctive_MAN/article/details/83622757
https://blog.csdn.net/songsir001/article/details/86311137
https://help.aliyun.com/document_detail/84798.html?spm=a2c4g.11186623.6.798.65df39a5DivjqP
https://blog.csdn.net/leimenghongchen1/article/details/52949175
posted @ 2020-05-22 15:08  伏沙金  阅读(719)  评论(0编辑  收藏  举报