no jpeg in java.library.path;java.lang.NoClassDefFoundError: Could not initialize class sun.awt.image.codec.JPEGImageEncoderImpl

因为要压缩图片,所以用到了JPEGImageEncoder类,但这个类在1.7后就被抛弃了,导致现在调用总是报错,虽然我在本地windows eclipse上可以成功调用,但到了centos上就出错了。

从网上的建议看,就是取而代之用ImageIO类就能解决这个问题。

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam; public class ImageUtil { // 压缩图片到制定的宽度,同时长度压缩到相同比例
private static final int COMPRESS_WIDTH = 150; // 压缩质量
private static final float RATIO = 0.99f;
/**
* 按宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传
* @param oldFile 要进行压缩的文件全路径
* @param newFile 新文件
* @param width 宽度
* @param height 高度
* @param quality 质量
* @return 返回压缩后的文件的全路径
*/
public static boolean compressImage(
String oldPath, String newPath) {
File oldFile = new File(oldPath);
File newFile = new File(newPath); if (!oldFile.exists()) {
return false;
} String newImage = null;
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(oldFile);
int w = srcFile.getWidth(null);
// System.out.println(w);
int h = srcFile.getHeight(null);
// System.out.println(h); /** 宽,高设定 */
// 压缩后的高
int height = (int)(1.0*COMPRESS_WIDTH/w*h);
BufferedImage tag = new BufferedImage(
COMPRESS_WIDTH, height
,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(srcFile, 0, 0, COMPRESS_WIDTH, height, null);
//String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
/** 压缩后的文件名 */
//newImage = filePrex + smallIcon+ oldFile.substring(filePrex.length()); /** 压缩之后临时存放位置 */ JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** 压缩质量 */
jep.setQuality(RATIO, true); // FileOutputStream out = new FileOutputStream(newFile);
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
// encoder.encode(tag, jep); String formatName = newPath.substring(newPath.lastIndexOf(".") + 1);
//FileOutputStream out = new FileOutputStream(dstName);
//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//encoder.encode(dstImage);
ImageIO.write(tag, /*"GIF"*/ formatName /* format desired */ , new File(newPath) /* target */ );
// out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}

no jpeg in java.library.path;java.lang.NoClassDefFoundError: Could not initialize class sun.awt.image.codec.JPEGImageEncoderImpl的更多相关文章

  1. exception java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment

      exception java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironmen ...

  2. java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment问题解决

    2018-09-29 17:45:16.905 ERROR [pool-1-thread-1]o.s.scheduling.support.TaskUtils$LoggingErrorHandler. ...

  3. java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment

    请求验证码时后台报错:java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment ...

  4. HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment

    解决方案:修改catalina.sh 文件加上-Djava.awt.headless=true JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS -Djava.awt.hea ...

  5. Java 上传文件到 SFTP 抛异常 java.lang.NoClassDefFoundError: Could not initialize class sun.security.ec.SunEC 的解决办法

    最近从 Op 那里报来一个问题,说是SFTP上传文件不成功.拿到的 Exception 如下: Caused by: java.lang.NoClassDefFoundError: Could not ...

  6. Linux下java验证码不显示:Could not initialize class sun.awt.X11FontManager

    一.问题 javaweb项目,登录的时候有个图片验证码的功能.在Windows本地测试能够正常显示,部署到Linux上就不行了.报错如下: org.springframework.web.util.N ...

  7. java.lang.UnsatisfiedLinkError: no XXX in java.library.path

    其中涉及的测试源码如下: For those who didn't install Javawith default settings, a systematic way for solving JN ...

  8. no leveldbjni64-1.8 in java.library.path

    在抽取以太坊Java版本的Trie树部分时,遇到了一个问题: Exception in thread "main" java.lang.UnsatisfiedLinkError: ...

  9. Error: java.lang.UnsatisfiedLinkError: no ntvinv in java.library.path

    Error Message When compiling or executing a Java application that uses the ArcObjects Java API, the ...

随机推荐

  1. Replication--修改复制代理配置来查看代理运行情况

    1>在复制监视器中选中订阅右键 2>选择代理配置文件 3>将代理配置文件设置为”详细历史记录代理配置文件“,确定以保存 4>重启代理 5>代理运行一段时间后,重启代理 6 ...

  2. Replication--无法将事务提升为分布式事务,因为在事务中有活动的保存点

    场景描述在SQL SERVER 2012上创建事务发布,发布库已搭建为可AWAYSON,分发服务器和发布服务器分离,创建发布时提示“无法将事务提升为分布式事务,因为在事务中有活动的保存点” 解决方法E ...

  3. 简单几步,提升.Net Core的开发效率

    附加IIS进程调式? 以前在开发ASP.NET(MVC)项目的时候,为了加快程序的启动速度(调式),我们会选择使用IIS.先用IIS架设还在开发的项目,在需要调式的时候附加进程,而在更多时候,如果调整 ...

  4. 南昌网络赛 I. Max answer 单调栈

    Max answer 题目链接 https://nanti.jisuanke.com/t/38228 Describe Alice has a magic array. She suggests th ...

  5. 【09】循序渐进学 docker:docker swarm

    写在前面的话 至此,docker 的基础知识已经了解的差不多了,接下来就来谈谈对于 docker 容器,我们如何来管理它. docker swarm 在学习 docker swarm 之前,得先知道容 ...

  6. 爬虫开发7.scrapy框架简介和基础应用

    scrapy框架简介和基础应用阅读量: 1432 scrapy 今日概要 scrapy框架介绍 环境安装 基础使用 今日详情 一.什么是Scrapy? Scrapy是一个为了爬取网站数据,提取结构性数 ...

  7. OCP新题库,052新加的考题及答案整理-24题

    24. YOUR DB_RECOVERY_FILE_DEST_SIZE Is 8G. Currently, 5G of the space Is used of which 4G consists o ...

  8. kvm虚拟化之kvm虚拟机控制台登陆

    vm虚拟机能否像xen虚拟机一样通过virsh console 一样采用字符界面进行linux虚拟机控制台呢,答案是肯定的,默认情况下该命令是不起作用的,需要修改相关文件才能实现. 1. 配置方法如下 ...

  9. Python strip()函数用法

    Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数: strip 同时去掉左右两边的空格lstrip 去掉左边的空格rstrip 去掉右边的空格 具体示 ...

  10. codis__通用的使用模式

    1,按功能模块分成不同的productName 参照 sample_user, sample_dynamic (见附件) sample_user.tar.gz,sample_dynamic.tar.g ...