java 图片压缩上传_Java - MultipartFile图片上传服务器,并且指定大小压缩

这篇博客介绍了如何使用Java处理MultipartFile类型的图片上传,并在服务器端进行指定大小的压缩。作者通过创建一个UploadFile方法,实现了从客户端上传图片到服务器,然后利用ImageIO和JPEGCodec进行图片压缩的操作。
摘要由CSDN通过智能技术生成

安卓新手,第一次写博客。 http://blog.csdn.net/h7870181/article/details/19971557 主要是学习了这个博文。 自己撸项目时候遇到了 图片上传的问题, 学习 肖赛SoAi 的博文 将问题解决。为了自己记得更熟,写了个上传图片的Demo ,希望能帮助大家。本人还是

http://blog.csdn.net/h7870181/article/details/19971557

2a78c600235926dcfc450f4b2b9054f9.gif

932a53af70fbac2a3c7ab7b8e19eff4b.gif

1 /***

2 * 上传图片到服务器 并压缩

3 *

4 * @param myFile 文件

5 * @param request

6 * @return

7 */

8 private Boolean UploadFile(MultipartFile myFile, int width, int height, HttpServletRequest request) {

9 Boolean sta = false;

10 InputStream is = null;

11 FileOutputStream fs = null;

12 /** 临时文件夹*/

13 String imgPath = "xyimages" + File.separator + "buttonImgTemp" + File.separator;

14 String tempPath = ServletConstants.WEB_ROOT_OS_PATH + imgPath;

15 System.out.println("old-path-" + tempPath);

16 String name = myFile.getOriginalFilename();

17 File oldFile = new File(tempPath);

18 if (!oldFile.exists()) {

19 oldFile.mkdirs();

20 }

21 /** 处理后文件夹*/

22 String newImaPath = "xyimages" + File.separator + "buttonImg" + File.separator;

23 String newPath = ServletConstants.WEB_ROOT_OS_PATH + newImaPath;

24 System.out.println("new-path-" + newPath);

25 File newFile = new File(newPath);

26 if (!newFile.exists()) {

27 newFile.mkdirs();

28 }

29 try {

30 /** 先存取源文件*/

31 is = myFile.getInputStream();

32 fs = new FileOutputStream(tempPath + myFile.getOriginalFilename());

33 //...

34 if (myFile.getSize() > 0) {

35 byte[] buffer = new byte[1024 * 1024];

36 int bytesum = 0;

37 int byteread = 0;

38 while ((byteread = is.read(buffer)) != -1) {

39 bytesum += byteread;

40 fs.write(buffer, 0, byteread);

41 fs.flush();

42 }

43 fs.close();

44 is.close();

45 }

46 /** 处理源文件 ,进行压缩再放置到新的文件夹*/

47 String oldPath = tempPath + myFile.getOriginalFilename();

48 String copyPath = newPath + myFile.getOriginalFilename();

49 Boolean ys = zipWidthHeightImageFile(oldPath, copyPath, width,height, 1f);

50 if (ys)

51 sta = true;

52 else

53 sta = false;

54 } catch (Exception ex) {

55 ex.printStackTrace();

56 sta = false;

57 }

58 return sta;

59 } 第一步,先存源图片

2a78c600235926dcfc450f4b2b9054f9.gif

932a53af70fbac2a3c7ab7b8e19eff4b.gif

1 /***

2 * 压缩制定大小图片

3 *

4 * @param oldPath 临时图片路径

5 * @param copyPath 压缩图片保存路径

6 * @param width 宽度

7 * @param height 高度

8 * @param quality 高清度

9 * @return

10 * @throws Exception

11 */

12 private Boolean zipWidthHeightImageFile(String oldPath, String copyPath, int width, int height,

13 float quality) {

14 Boolean sta = false;

15 File oldFile = new File(oldPath);

16 File newFile = new File(copyPath);

17 if (oldFile == null) {

18 return null;

19 }

20 String newImage = null;

21 try {

22 /** 对服务器上的临时文件进行处理 */

23 Image srcFile = ImageIO.read(oldFile);

24 int w = srcFile.getWidth(null);

25 System.out.println(w);

26 int h = srcFile.getHeight(null);

27 System.out.println(h);

28

29 /** 宽,高设定 */

30 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

31 tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);

32 //String filePrex = oldFile.substring(0, oldFile.indexOf('.'));

33 /** 压缩后的文件名 */

34 //newImage = filePrex + smallIcon+ oldFile.substring(filePrex.length());

35

36 /** 压缩之后临时存放位置 */

37 FileOutputStream out = new FileOutputStream(newFile);

38

39 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

40 JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);

41 /** 压缩质量 */

42 jep.setQuality(quality, true);

43 encoder.encode(tag, jep);

44 out.close();

45 sta = true;

46 } catch (Exception e) {

47 e.printStackTrace();

48 sta = false;

49 }

50 return sta;

51 } 压缩图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值