求助:如何把图片写到jsp页面上 我想问问各位大虾,如何往jsp页面写图片,而不是采用链接方式做?请大虾赐教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int width=60, height=20;BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取图形上下文Graphics g = image.getGraphics();//生成随机类Random random = new Random();// 设定背景色g.setColor(getRandColor(200,250));g.fillRect(0, 0, width, height);//设定字体g.setFont(new Font("宋体",Font.PLAIN,18));//画边框//g.setColor(new Color());//g.drawRect(0,0,width-1,height-1);// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到g.setColor(getRandColor(160,200));for (int i=0;i<155;i++){ int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x,y,x+xl,y+yl);}// 取随机产生的认证码(4位数字)String sRand="";for (int i=0;i<4;i++){ String rand=String.valueOf(random.nextInt(10)); sRand+=rand; // 将认证码显示到图象中 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawString(rand,13*i+6,16);}// 将认证码存入SESSIONsession.setAttribute("rand",sRand);// 图象生效g.dispose();// 输出图象到页面ImageIO.write(image, "JPEG", response.getOutputStream());%> String fileName = "c:/Milan.gif"; File f = new File(fileName); if (!f.exists()) { response.sendError(404, "File not found!"); return; } response.reset(); URL u = new URL("file:///" + fileName); response.setContentType(u.openConnection().getContentType()); response.setHeader("Content-Disposition", "inline; filename=" + f.getName()); byte[] buf = new byte[1024]; int len = 0; BufferedInputStream br = null; OutputStream output = null; try { br = new BufferedInputStream(new FileInputStream(f)); output = response.getOutputStream(); while ((len = br.read(buf)) > 0) { output.write(buf, 0, len); } output.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { br.close(); br = null; } if (output != null) { output.close(); output = null; } } 图片一定要用HTML标签的,可以用IMG,也可以用其他标签,设置background属性但能直接写到jsp里,jsp最终是要转换成HTML纯文本到客户端的至于连接到具体的图片文件还是什么Servlet,jsp之类的,只要能获取图片的文件流就行了可以参考楼上的,用servlet<img src="你的servlet"> <img src="xxxx.xxxx.xxxx.xxx" />类里面:response.setContentType("image/*"); 楼上这个<img src="xxxx.xxxx.xxxx.xxx" />不就可以么,还要什么样子的? Swing JList动态的问题 equals问题 大家帮我解决下,谢谢 java 正则 正向预搜索( 求助"火龙果" ) 如何让JLabel的文本换行显示 String数据类型由一个类来定义,那么请问... 关于ANT的命令行执行 有个问题想问 为什么我在windows下的project不能在linux下运行? 怎样设置JTable的颜色??? 初级问题,哪位大哥给看看~~~~ 默认构建器问题
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取图形上下文
Graphics g = image.getGraphics();//生成随机类
Random random = new Random();// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);//设定字体
g.setFont(new Font("宋体",Font.PLAIN,18));//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 将认证码显示到图象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}// 将认证码存入SESSION
session.setAttribute("rand",sRand);// 图象生效
g.dispose();// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
String fileName = "c:/Milan.gif"; File f = new File(fileName); if (!f.exists()) {
response.sendError(404, "File not found!");
return;
} response.reset(); URL u = new URL("file:///" + fileName);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="
+ f.getName()); byte[] buf = new byte[1024];
int len = 0;
BufferedInputStream br = null;
OutputStream output = null;
try {
br = new BufferedInputStream(new FileInputStream(f));
output = response.getOutputStream();
while ((len = br.read(buf)) > 0) {
output.write(buf, 0, len);
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
br.close();
br = null;
}
if (output != null) {
output.close();
output = null;
}
}
但能直接写到jsp里,jsp最终是要转换成HTML纯文本到客户端的
至于连接到具体的图片文件还是什么Servlet,jsp之类的,只要能获取图片的文件流就行了
可以参考楼上的,用servlet
<img src="你的servlet">
response.setContentType("image/*");