zoukankan      html  css  js  c++  java
  • Java

     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     } 
    第一步,先存源图片
     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     }
    压缩图片
  • 相关阅读:
    golang 简易聊天
    golang 自定义封包协议(转的)
    PHP 7 测试用例(转)
    android 百度地图开发
    Android studio 签名使用转
    百度ueditor 拖文件或world 里面复制粘贴图片到编辑中 上传到第三方问题
    Android控件属性大全(转)
    Android studio 一个项目中添加两个module遇到的bug
    win7 64位DCOM配置(关于导出excel 配置计算机组件服务)(转)
    Python学习--07迭代器、生成器
  • 原文地址:https://www.cnblogs.com/huanzei/p/5113883.html
Copyright © 2011-2022 走看看