java 后台图片压缩上传png适用

来源:互联网 发布:目标检测 知乎 编辑:程序博客网 时间:2024/06/05 13:28
/** * @param oldFile 要进行压缩的文件全路径 * @param width 压缩后的宽度 * @param height 压缩后的高度 * @param newFile 压缩后的文件的全路径 * @return 返回压缩后的文件的全路径 */public static String zipImageFile(String oldFile, int width, int height, String newFile) {if (oldFile == null) {return null;}String newImage = null;try {/**对服务器上的临时文件进行处理 */Image srcFile = ImageIO.read(new File(oldFile));/** 宽,高设定 */int bImgType=BufferedImage.TYPE_INT_RGB;String prefix=oldFile.substring(oldFile.lastIndexOf(".")+1);if("png".equals(prefix)){bImgType=BufferedImage.TYPE_INT_ARGB;}BufferedImage tag = new BufferedImage(width, height, bImgType);tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);ImageIO.write(tag, prefix, new File(newFile));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return newImage;}

0 0