/**
* 获取上传文件
*
* @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

压缩并上传图片到阿里云(jfinal)的更多相关文章

  1. 上传图片到阿里云OSS和获取上传图片的外网url的步骤

    啥都不说  直接上代码 1.html: <form action="/bcis/api/headImgUpload.json" method="post" ...

  2. swift3.0 从相册选取或者拍照上传图片至阿里云OSS

    导入相应的库 import Photos import AliyunOSSiOS 选取照片需要继承 UIImagePickerControllerDelegate,UINavigationContro ...

  3. multipartUpload上传图片到阿里云

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. vue 上传图片到阿里云(前端直传:不推荐)

    为何要这样做:减轻后端数据库压力(个人觉得于前端没啥用,谁返回来都行) 代码部分: <template> <div class="upLoad"> < ...

  5. Java中使用RestFul接口上传图片到阿里云OSS服务器

    1.接口方法 import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.springfr ...

  6. vue中上传图片至阿里云oss

    1.开通阿里云的oss服务这些这里就不多做介绍了 2.登入阿里云的后台管理系统创建一个Bucket 3.在后台管理系统中进入访问控制 4.点击用户管理->新建用户->填写相关信息,就生成了 ...

  7. 上传图片到阿里云oss

    阿里云地址 登录阿里云管理控制台,创建对象存储oss private static final String endpoint = "http://oss-cn-shanghai.aliyu ...

  8. TP框架上传图片至阿里云oss

    首先安装阿里云oss扩展: composer require aliyuncs/oss-sdk-php 如果这个安装不上可以直接下载SDK的包: 链接:https://pan.baidu.com/s/ ...

  9. PHP上传图片至阿里云

    <?php header("Content-type: text/html; charset=utf-8"); header('Access-Control-Allow-Or ...

随机推荐

  1. Express4.x之API:express

    express()表达式 express的方法 express功能分析 一.express()表达式 创建Express应用程序.express()函数是express模块导出的顶级函数.(相当于HT ...

  2. python工业互联网应用实战2—从需求开始

    前言:随着国家工业2025战略的推进,工业互联网发展将会提速,将迎来一个新的发展时期,越来越多的企业开始逐步的把产线自动化,去年年底投产的小米亦庄的智能工厂就是一个热议的新闻.小米/华为智能工厂只能说 ...

  3. phpstorm设置xdebug调试

    phpstorm设置xdebug调试# wamp开发环境安装完成以后,打开网页,输入 :localhost 检测xdebug是否开启 3.若xdebug已开启,请找到你wamp或者phpstudy的安 ...

  4. Pikachu靶场SQL注入刷题记录

    数字型注入 0x01 burp抓包,发送至repeater 后面加and 1=1,and 1=2 可判断存在注入 0x02 通过order by判断字段数,order by 2 和order by 3 ...

  5. Excel帮助类

    Excel帮助类操作 public class ExcelHelper { /// <summary> /// 将xls导入List /// </summary> /// &l ...

  6. 堆、栈、数据区、bss、代码段

    一个程序的运行是需要内存的,那么我们平常写的程序的内存都是怎么分配的呢 (1)首先我们要知道,内存是真实存在的,内存是一个物理器件.它时由操作系统管理的,我们平常只要使用它就行了,为了方便管理.操作系 ...

  7. 文档翻译经验分享(Markdown)

    该教程基于VSCode 加一些插件 youdao translate https://marketplace.visualstudio.com/items?itemName=Yao-Translate ...

  8. 数据库连接池 --Druid 连接工具类创建_JDBCUtils

    package com.itheima.jdbc_druid; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.s ...

  9. win10 64位 MySQL 8.0 下载与安装

    免安装版(超级棒的教程): 安装只需 Download .zip压缩文件 卸载只需 Delete 解压文件即可 https://blog.csdn.net/hzxOnlineOk/article/de ...

  10. 一.5.序列化应用之服务器制造厂与型号app功能

    1.环境准备: (python36env) [vagrant@CentOS7 apps]$ django-admin startapp manufacturer (1)激活:'manufacturer ...