javax异常: javax.imageio.IIOException: Can't create output stream解决方法
- 验证码工具类:
package com.newcapec.utils;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
/**
* <p>Title: ValidateCode.java</p>
* <p>Description: 验证码工具类</p>
*/
public class ValidateCode {
// 图片的宽度。
private int width = 120;
// 图片的高度。
private int height = 40;
// 验证码字符个数
private int codeCount = 4;
// 验证码干扰线数
private int lineCount = 30;
// 验证码
private String code = null;
// 验证码图片Buffer
private BufferedImage buffImg = null;
private char[] codeSequence = { 'A','a', 'B','b', 'C','c', 'D','d', 'E','e', 'F','f', 'g','H','h',
'J','j', 'K','k','L', 'M','m', 'N','n', 'P','p', 'Q','q', 'R','r', 'S','s', 'T', 'U','u', 'V', 'W','w',
'X','x', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'};
// 生成随机数
private Random random = new Random();
public ValidateCode() {
this.createCode();
}
/**
*
* @param width
* 图片宽
* @param height
* 图片高
*/
public ValidateCode(int width, int height) {
this.width = width;
this.height = height;
this.createCode();
}
/**
*
* @param width
* 图片宽
* @param height
* 图片高
* @param codeCount
* 字符个数
* @param lineCount
* 干扰线条数
*/
public ValidateCode(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.createCode();
}
public void createCode() {
Random heightRandom = new Random();
int codeX = 0;
int fontHeight = 0;
fontHeight = height-heightRandom.nextInt(7)-7;// 字体的高度
codeX = width / (codeCount+1);// 每个字符的宽度
// 图像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 创建字体
ImgFontByte imgFont = new ImgFontByte();
Font font = imgFont.getFont(fontHeight);
g.setFont(font);
// 绘制干扰线
for (int i = 0; i < lineCount; i++) {
int xs = getRandomNumber(width);
int ys = getRandomNumber(height);
int xe = xs + getRandomNumber(width / 8);
int ye = ys + getRandomNumber(height / 8);
g.setColor(getRandomColor());
g.drawLine(xs, ys, xe, ye);
}
StringBuffer randomCode = new StringBuffer();
// int x = 5;
// 随机产生验证码字符
for (int i = 0; i < codeCount; i++) {
int h = height-8;
int w = (i + 1) * codeX+heightRandom.nextInt(4);
String strRand = String.valueOf(codeSequence[random
.nextInt(codeSequence.length)]);
// 设置字体颜色
g.setColor(getRandomColor());
int degree = new Random().nextInt() % 10;
g.rotate(degree * Math.PI / 180,(w+codeX-5)/2, (h+fontHeight)/2);
// 设置字体位置
g.drawString(strRand, w, h ); //getRandomNumber(height / 2) + 25
g.rotate(-degree * Math.PI / 150, (w+codeX-5)/2, (h+fontHeight)/2);
randomCode.append(strRand);
}
code = randomCode.toString();
}
/** 获取随机颜色 */
private Color getRandomColor() {
int r = getRandomNumber(225);
int g = getRandomNumber(225);
int b = getRandomNumber(225);
return new Color(r, g, b);
}
/** 获取随机数 */
private int getRandomNumber(int number) {
return random.nextInt(number);
}
public void write(String path) throws IOException {
OutputStream sos = new FileOutputStream(path);
this.write(sos);
}
public void write(OutputStream sos) throws IOException {
// ImageIO.write(buffImg, "png", sos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
encoder.encode(buffImg);
sos.close();
}
public BufferedImage getBuffImg() {
return buffImg;
}
public String getCode() {
return code;
}
/** 字体样式类 */
class ImgFontByte {
public Font getFont(int fontHeight) {
return new Font("Arial", Font.PLAIN, fontHeight);
}
}
}
- 使用
public void scaptcha(){
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
// 设置响应的类型格式为图片格式
response.setContentType("image/jpeg");
// 禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
ValidateCode instance = new ValidateCode();
CookieUtil.setCookie(response, "scaptcha", instance.getCode().toUpperCase(), null, -1);
try {
instance.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
在使用ImageIO.write时,发现在Linux平台上,会出现异常:
javax.imageio.IIOException: Can't create output stream
检查tomcat的日志,终于真相大白:
javax.imageio.IIOException: Can't create output stream!
at javax.imageio.ImageIO.write(ImageIO.java:1521)
Caused by: javax.imageio.IIOException: Can't create cache file!
at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:395)
at javax.imageio.ImageIO.write(ImageIO.java:1519)
... 34 more
Caused by: java.io.IOException: 系统找不到指定的路径。
原来是ImageIO.write(image, "jpeg", response.getOutputStream());
查看日志,发现是由找不到文件引起
Java.nio.file.NoSuchFileException: xxx.../temp/imageio4138671232726624650.tmp
- 主要原因如下:
在使用ImageIO进行图片写操作时,默认会使用缓存目录:${tomcat}/temp,在此缓存目录会生成缓存文件imageio4138671232726624650.tmp(这一串数字应该是当前时间戳,临时文件名),有些生产环境的tomcat,会将temp目录删除,因此报错
- 4种解决方法如下:
- 在tomcat下新建temp目录;
- 与方法1相似,通过ImageIO.setCacheDirectory(cacheDirectory);设置任意的、存在的缓存目录
- ImageIO默认是使用缓存目录,可以通过ImageIO.setUseCache(false)来设置,更改缓存策略,不使用文件目录缓存,使用内存缓存
- 不使用ImageIO,换成其它JDK方法
ImageIO.write(bi, "jpg", baos);
换成:
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(image);
javax异常: javax.imageio.IIOException: Can't create output stream解决方法相关推荐
- 计算机开启时提示键盘错误,电脑开机出现异常提示keyboard not found的故障原因及解决方法_电脑故障...
电脑开机后屏幕显示keyboard not found. press f2 to continue .f1 to setup,具体问题现象如下所示: 故障原因分析: 出现这样的情况大多都是电脑在开机的 ...
- vs变量监视提示-VAR-CREATE: UNABLE TO CREATE VARIABLE OBJECT解决方法
vs变量监视提示-VAR-CREATE: UNABLE TO CREATE VARIABLE OBJECT解决方法 参考文章: (1)vs变量监视提示-VAR-CREATE: UNABLE TO CR ...
- 网络异常无法连接远程服务器,《Chess Rush》网络异常进不去怎么回事 无法连接服务器解决方法...
导 读 Chess Rush突然弹出网络异常无法连接服务器要怎么处理?很多玩家都遇到登录不上游戏的情况,遇到这种问题怎么办?这款游戏海外已经开始下载,由于玩家人数多出现了不少的问题,下面帮大家解决网络 ...
- U3d引擎崩溃、异常、警告、BUG与提示总结及解决方法
此贴会持续更新,都是项目中常会遇到的问题,总结成贴,提醒自己和方便日后检查,也能帮到有需要的同学. 若各位有啥好BUG好异常好警告好崩溃可以分享的话,请多多指教.xuzhiping7#qq.com. ...
- jmeter 运行接口报javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection的解决方法
今天上午,一位小伙伴(jmeter初学者)在自己抓包后,将接口放到jmeter中,进行接口测试时,出现这样的报错: javax.net.ssl.SSLException: Unrecognized S ...
- mysql导入数据io异常_mysql 数据同步 出现Slave_IO_Running:No问题的解决方法小结
mysql replication 中slave机器上有两个关键的进程,死一个都不行,一个是slave_sql_running,一个是Slave_IO_Running,一个负责与主机的io通信,一个负 ...
- 深度deepin20打印失败“Can’t create temporary file”解决方法
深度20beta下,USB连接HP打印机,自动识别成功,但打印时提示"Can't create temporary file",目测是权限问题. 1.检查权限 getfacl /v ...
- SQL Developer Unable to create an instance ...解决方法
下载解压了Oracle SQL Developer工具,运行时,启动不了,报错信息如下: --------------------------- Unable to create an instanc ...
- java 非法操作异常_电脑提示非法操作怎么办 电脑系统故障解决方法【详解】
电脑提示非法操作怎么办? 在电脑操作中,出现"非法操作"提示的几率比蓝屏现象要多出一筹.造成"非法操作"的原因主要出自软件.当一个程序访问其内存地址空间之外的内 ...
最新文章
- MySQL/phpmyadmin问题解决手记:#2002 – 服务器没有响应 (或者本地 MySQL 服务器的套接字没有正确配置)
- Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
- 蔚来ES8停车场蛇形走位 连撞数车 官方回应:未证明是车辆失控
- awk if 不包含_linux三剑客之awk
- 程序员的技术负债怎么还?
- 哈萨比斯首次解读AlphaZero竟被当场diss,他起身当面回击说…
- ubuntu安装Nvidia-docker2详细步骤
- 输出IMG格式SAR图像——Img格式图像文件概述
- c语言c98和c99,c99和c98的差异
- 如何清理和删除 Docker 镜像
- 【GDOI模拟】屏保
- H264编码格式--图文解释
- 「Mac小技巧」教你如何解决WiFi的国家地区代码冲突
- comsol积分函数_空间与时间的积分方法概述
- Ghostexp.exe
- 【平衡小车制作】(四)陀螺仪MPU6050(超详解)
- python爬取千图网_python 爬取 花瓣网图片,千图网图片
- DBMS_AUDIT_MGMT.FLUSH_UNIFIED_AUDIT_TRAIL过程
- IOS 企业级苹果开发者账号申请流程
- SQL报错及解决方法(随缘更新)