java上传图片压缩大小限制_java实现上传图片并压缩图片大小功能

/***

* @Description:保存图片并且生成缩略图

*@paramimageFile 图片文件

*@paramrequest 请求对象

*@paramuploadPath 上传目录

*@return*/public staticBaseResult uploadFileAndCreateThumbnail(MultipartFile imageFile,HttpServletRequest request,String uploadPath) {if(imageFile == null){return new BaseResult(false, "imageFile不能为空");

}if (imageFile.getSize() >= 10*1024*1024)

{return new BaseResult(false, "文件不能大于10M");

}

String uuid=UUID.randomUUID().toString();

String fileDirectory= CommonDateUtils.date2string(newDate(), CommonDateUtils.YYYY_MM_DD);//拼接后台文件名称

String pathName = fileDirectory + File.separator + uuid + "."+FilenameUtils.getExtension(imageFile.getOriginalFilename());//构建保存文件路劲//2016-5-6 yangkang 修改上传路径为服务器上

String realPath = request.getServletContext().getRealPath("uploadPath");//获取服务器绝对路径 linux 服务器地址 获取当前使用的配置文件配置//String urlString=PropertiesUtil.getInstance().getSysPro("uploadPath");//拼接文件路劲

String filePathName = realPath + File.separator +pathName;

log.info("图片上传路径:"+filePathName);//判断文件保存是否存在

File file = newFile(filePathName);if (file.getParentFile() != null || !file.getParentFile().isDirectory()) {//创建文件

file.getParentFile().mkdirs();

}

InputStream inputStream= null;

FileOutputStream fileOutputStream= null;try{

inputStream=imageFile.getInputStream();

fileOutputStream= newFileOutputStream(file);//写出文件//2016-05-12 yangkang 改为增加缓存//IOUtils.copy(inputStream, fileOutputStream);

byte[] buffer = new byte[2048];

IOUtils.copyLarge(inputStream, fileOutputStream, buffer);

buffer= null;

}catch(IOException e) {

filePathName= null;return new BaseResult(false, "操作失败", e.getMessage());

}finally{try{if (inputStream != null) {

inputStream.close();

}if (fileOutputStream != null) {

fileOutputStream.flush();

fileOutputStream.close();

}

}catch(IOException e) {

filePathName= null;return new BaseResult(false, "操作失败", e.getMessage());

}

}//String fileId = FastDFSClient.uploadFile(file, filePathName);

/*** 缩略图begin*///拼接后台文件名称

String thumbnailPathName = fileDirectory + File.separator + uuid + "small."+FilenameUtils.getExtension(imageFile.getOriginalFilename());//added by yangkang 2016-3-30 去掉后缀中包含的.png字符串

if(thumbnailPathName.contains(".png")){

thumbnailPathName= thumbnailPathName.replace(".png", ".jpg");

}long size =imageFile.getSize();double scale = 1.0d;if(size >= 200*1024){if(size > 0){

scale= (200*1024f) /size ;

}

}//拼接文件路劲

String thumbnailFilePathName = realPath + File.separator +thumbnailPathName;try{//added by chenshun 2016-3-22 注释掉之前长宽的方式,改用大小//Thumbnails.of(filePathName).size(width, height).toFile(thumbnailFilePathName);

if(size < 200*1024){

Thumbnails.of(filePathName).scale(1f).outputFormat("jpg").toFile(thumbnailFilePathName);

}else{

Thumbnails.of(filePathName).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(thumbnailFilePathName);

}

}catch(Exception e1) {return new BaseResult(false, "操作失败", e1.getMessage());

}/*** 缩略图end*/Map map = new HashMap();//原图地址

map.put("originalUrl", pathName);//缩略图地址

map.put("thumbnailUrl", thumbnailPathName);return new BaseResult(true, "操作成功", map);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值