关于验证码的问题
使用了以前学过的验证码,正好复习,但是遇到在idea中报IO异常 eclipse却完美运行
javax.imageio.IIOException: Can't create output stream! at javax.imageio.ImageIO.write(ImageIO.java:1574) at com.imooc.code.CaptcahCode.drawImage(CaptcahCode.java:65) at org.apache.jsp.code_jsp._jspService(code_jsp.java:131) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) Caused by: javax.imageio.IIOException: Can't create cache file! at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:423) at javax.imageio.ImageIO.write(ImageIO.java:1572) ... 30 more Caused by: java.nio.file.AccessDeniedException: C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp\imageio805514725752775181.tmp at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230) at java.nio.file.Files.newByteChannel(Files.java:361) at java.nio.file.Files.createFile(Files.java:632) at java.nio.file.TempFileHelper.create(TempFileHelper.java:138) at java.nio.file.TempFileHelper.createTempFile(TempFileHelper.java:161) at java.nio.file.Files.createTempFile(Files.java:897) at javax.imageio.stream.FileCacheImageOutputStream.<init>(FileCacheImageOutputStream.java:88) at com.sun.imageio.spi.OutputStreamImageOutputStreamSpi.createOutputStreamInstance(OutputStreamImageOutputStreamSpi.java:68) at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:419) ... 31 more
//这是报错地方的代码 try { ImageIO.write(bi,"jpg",response.getOutputStream()); //就是这行报错 //刷新响应流 response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); }
31
收起
正在回答
4回答
ImageIO.setCacheDirectory(cacheDirectory);前面加上这句话,方法参数是个目录,就是重新设置一下cache目录试试。因为从报错信息看,默认的Tomcat下的temp目录是拒绝访问的。
祝学习愉快!
荒小北158
2018-05-07 18:16:18
package com.imooc.code; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Random; /** * 产生验证码工具类 */ public class CaptcahCode { /** * 生成验证码图片的方法 * @param response 需要一个响应流,将图片输出到JSP页面显示 * @return */ public static String drawImage(HttpServletResponse response) { //需求是:需要随机数,那么我们单独的把随机数抽离成一个产生随机数的方法 //1、定义以字符串拼接的StringBuilder StringBuilder builder = new StringBuilder(); //2、准备产生4个字符串的随机数(这里做一个循环,在循环里面拼接) for (int i = 0; i < 4; i++) { //该方法用于拼接 builder.append(randomChar()); } String code = builder.toString(); //这是随机的字符数字组合 //3、定义图片的宽度和高度 int width = 70; int height = 25; //建立bufferedImage对象,制定图片的长度和宽度以及色彩(子类描述具有可访问图像数据缓冲区的 Image) //图片最终都会写入到缓冲流中 BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); //4、获取到Graphics2D 绘制对象,开始绘制验证码 Graphics2D g = bi.createGraphics(); //5、设置文字的字体和字体大小 Font font = new Font("微软雅黑",Font.PLAIN,20); //设置字体的颜色 Color color = new Color(0,0,0); g.setFont(font); //设置字体 g.setColor(color); //设置颜色 g.setBackground(new Color(226,226,240)); //设置背景 //开始绘制对象(先调用clearRect清除 再重画) g.clearRect(0,0,width,height); //验证码是一个矩形 我们需要一个矩形对象 然后将图片对象填充进矩形框中 //我们要将内容放到这个矩形框里面 FontRenderContext context = g.getFontRenderContext(); //这是获取上下文的对象 Rectangle2D bounds = font.getStringBounds(code,context); //矩形拿到字体的上下文,并渲染 //计算文件的坐标和间距 double x = (width - bounds.getWidth()) / 2; //以 double 精度返回窗体矩形的宽度。 double y = (height - bounds.getHeight()) / 2; //以 double 精度返回窗体矩形的高度。 double ascent = bounds.getY(); //以 double 精度返回窗体矩形左上角的 Y 坐标。 double baseY = y - ascent; //开始绘制 g.drawString(code,(int)x,(int)baseY); //结束绘制 g.dispose(); //生成验证码最终的代码: try { File cacheDirectoey = new File("cache"); if(!cacheDirectoey.exists()) { cacheDirectoey.mkdir(); } ImageIO.setCacheDirectory(cacheDirectoey); ImageIO.write(bi,"jpg",response.getOutputStream()); //刷新响应流 response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } return code; } /** * 此方法用于产生随机数字和字母 * @return singleChar */ private static char randomChar() { //1、定义验证码需要的字母和数字 String string = "QWERTYUIOPLKJHMNBGFVDSXA0123456789"; //2、定义随机对象 Random random = new Random(); //3、产生索引的随机数,在字符长度范围内随机选取 int index = random.nextInt(string.length()); //4、根据随机索引截取string中的某一个字符 char singleChar = string.charAt(index); //5、返回值 return singleChar; } }
//////这是code.jsp <%@ page import="com.imooc.code.CaptcahCode" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% //1、清空浏览器缓存,目的是为了清空浏览器的缓存,因为浏览器 //会对网站的资源文件和图像进行记忆存储,如果被浏览器加载过的图片就记忆起来,记忆以后 //文件就不会和服务器在交互,如果我们验证不清空的话可能会造成一个问题就是:验证刷新以后没有效果 response.setHeader("prama","no-cache"); response.setHeader("cache-control","no-cache"); response.setHeader("expires","0"); //2、调用编写的生成验证码的工具类 String code = CaptcahCode.drawImage(response); session.setAttribute("code",code); //3、如何解决getOutputStram异常问题 out.clear(); out = pageContext.pushBody(); %>
////这是index.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>验证码</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <img alt="无法显示" src="code.jsp"> </body> </html>
荒小北158
2018-05-07 12:36:32
//生成验证码最终的代码: try { File cacheDirectoey = new File("cache"); if(!cacheDirectoey.exists()) { cacheDirectoey.createNewFile(); } ImageIO.setCacheDirectory(cacheDirectoey); ImageIO.write(bi,"jpg",response.getOutputStream()); //刷新响应流 response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); }
这样改么?
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星