- 浏览: 29200 次
- 性别:
- 来自: 北京
最新评论
-
yoyozizou:
imjl 写道呵呵,你看得lucene in action是中 ...
lucene in action笔记之一索引 -
xucons:
修改完http.conf后,最好在cmd中运行一下httpd. ...
apache+php+mysql 部署遇到的问题 -
javawxl:
学习!!!
利用独立的servlet在页面显示图片 -
apchy:
写的不错,以前也实现过相似功能,但没不完善,收藏了
利用独立的servlet在页面显示图片 -
liangguanhui:
这样生成的缩略图貌似很粗糙的。
利用独立的servlet在页面显示图片
web页面上的图片显示可以指定了一个url, 直接访问后台的一个servlet. 在后台数据库可以专门建一个图片库. 以下是一个简单的示例.在这里采用了imageJ-v1.37项目(网址: http://rsb.info.nih.gov/ij/ ) 进行图片处理.
1, ImageViewServlet
java 代码
- package com.hank.web.servlet;
- import java.io.IOException;
- import java.io.OutputStream;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- import com.hank.model.ConAttachs;
- import com.hank.service.ConAttachsManager;
- import com.hank.util.ImageUtil;
- /**
- * @author hank
- * @date 2007-9-12 03:49:57
- * @version
- */
- public class ImageViewServlet extends HttpServlet {
- private static final long serialVersionUID = 714920135153152003L;
- protected final transient Log log = LogFactory.getLog(getClass());
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- execute(request, response);
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- execute(request, response);
- }
- public void execute(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- String[] infos = request.getPathInfo().split("/");
- String name=infos[1];
- String id=infos[2].substring(0, infos[2].lastIndexOf('.'));
- OutputStream out = response.getOutputStream();
- ApplicationContext ctx =
- WebApplicationContextUtils.getWebApplicationContext(getServletContext());
- if (name.equals("conAttach")){
- ConAttachsManager mgr = (ConAttachsManager)ctx.getBean("conAttachsManager");
- ConAttachs conAttachs = mgr.getConAttachsByContextId(id);
- if(conAttachs.getFType().intValue()==1){
- return;
- }
- byte[] images = conAttachs.getContextFile();
- try {
- response.setContentType(conAttachs.getFhttpContent());//设置contentType
- ImageUtil.outLogo(images,out,200,100);//显示200*100的缩略图
- response.getOutputStream().flush();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- if(out!=null){
- out.close();
- }
- }
- }else if(){
- ... ...
- }
- }
- }
2, ImageUtil
java 代码
- package com.hank.util;
- import ij.ImagePlus;
- import ij.io.Opener;
- import ij.process.Blitter;
- import ij.process.ImageProcessor;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.sun.image.codec.jpeg.ImageFormatException;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- /**
- * @author hank
- * @date 2007-9-12 03:14:21
- * @version
- */
- public class ImageUtil {
- protected final transient Log log = LogFactory.getLog(getClass());
- public static void outLogo(byte[] source,OutputStream out,int dwidth,int dheight) throws Exception{
- BufferedInputStream stream = new BufferedInputStream((new ByteArrayInputStream(source)),8092);//控制流速
- Image src = javax.imageio.ImageIO.read(stream);
- int width=src.getWidth(null);
- int height=src.getHeight(null);
- int towidth,toheight;
- if (width>dwidth || height>dheight){
- if (((float)width/dwidth)>=((float)height/dheight)){
- towidth = dwidth;
- toheight = (height*dwidth)/width;
- }else{
- toheight = dheight;
- towidth = (width* dheight)/height;
- }
- }else{
- towidth= width;
- toheight=height;
- }
- BufferedImage tag = new BufferedImage(towidth,toheight,BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(src,0,0,towidth,toheight,null);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag);
- }
- }
3 ,在web.xml中配置 servlet
4 , 页面上直接使用img标签指定src即可
- ij.jar (1 MB)
- 描述: imageJ包
- 下载次数: 270
评论
5 楼
javawxl
2008-04-30
学习!!!
4 楼
apchy
2008-04-26
写的不错,以前也实现过相似功能,但没不完善,收藏了
3 楼
liangguanhui
2008-04-08
这样生成的缩略图貌似很粗糙的。
2 楼
wy36928
2008-04-06
<div class='quote_title'>hank 写道</div><div class='quote_div'><p> web页面上的图片显示可以指定了一个url, 直接访问后台的一个servlet. 在后台数据库可以专门建一个图片库. 以下是一个简单的示例.在这里采用了imageJ-v1.37项目(网址: <span style='font-family: Arial;'><a href='http://rsb.info.nih.gov/ij/'>http://rsb.info.nih.gov/ij/</a> ) </span>进行图片处理.</p><p> </p><p>1, ImageViewServlet </p><div class='code_title'>java 代码</div><div class='dp-highlighter'><ol class='dp-j'><li class='alt'><span><span class='keyword'>package</span><span> com.hank.web.servlet; </span></span> </li><li><span> </span> </li><li class='alt'><span class='keyword'>import</span><span> java.io.IOException; </span> </li><li/><li><span class='keyword'>testtest </span></li><li><span class='keyword'>import</span><span> javax.servlet.http.HttpServletRequest; </span> </li><li class='alt'><span class='keyword'>import</span><span> javax.servlet.http.HttpServletResponse; </span> </li><li><span> </span> </li><li class='alt'><span class='keyword'>import</span><span> org.apache.commons.logging.Log; </span> </li><li><span class='keyword'>import</span><span> org.apache.commons.logging.LogFactory; </span> </li><li class='alt'><span class='keyword'>import</span><span> org.springframework.context.ApplicationContext; </span> </li><li><span class='keyword'>import</span><span> org.springframework.web.context.support.WebApplicationContextUtils; </span> </li><li class='alt'><span> </span> </li><li><span class='keyword'>import</span><span> com.hank.model.ConAttachs; </span> </li><li class='alt'><span class='keyword'>import</span><span> com.hank.service.ConAttachsManager; </span> </li><li><span class='keyword'>import</span><span> com.hank.util.ImageUtil; </span> </li><li class='alt'><span> </span> </li><li><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * @author hank </span> </span> </li><li><span><span class='comment'> * @date 2007-9-12 03:49:57 </span> </span> </li><li class='alt'><span><span class='comment'> * @version </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span class='keyword'>public</span><span> </span><span class='keyword'>class</span><span> ImageViewServlet </span><span class='keyword'>extends</span><span> HttpServlet { </span> </li><li><span> </span><span class='keyword'>private</span><span> </span><span class='keyword'>static</span><span> </span><span class='keyword'>final</span><span> </span><span class='keyword'>long</span><span> serialVersionUID = 714920135153152003L; </span> </li><li class='alt'><span> </span><span class='keyword'>protected</span><span> </span><span class='keyword'>final</span><span> </span><span class='keyword'>transient</span><span> Log log = LogFactory.getLog(getClass()); </span> </li><li><span> </span><span class='annotation'>@Override</span><span> </span> </li><li class='alt'><span> </span><span class='keyword'>protected</span><span> </span><span class='keyword'>void</span><span> doGet(HttpServletRequest request, HttpServletResponse response) </span><span class='keyword'>throws</span><span> ServletException, IOException { </span> </li><li><span> execute(request, response); </span> </li><li class='alt'><span> } </span> </li><li><span> </span> </li><li class='alt'><span> </span><span class='annotation'>@Override</span><span> </span> </li><li><span> </span><span class='keyword'>protected</span><span> </span><span class='keyword'>void</span><span> doPost(HttpServletRequest request, HttpServletResponse response) </span><span class='keyword'>throws</span><span> ServletException, IOException { </span> </li><li class='alt'><span> execute(request, response); </span> </li><li><span> } </span> </li><li class='alt'><span> </span> </li><li><span> </span><span class='keyword'>public</span><span> </span><span class='keyword'>void</span><span> execute(HttpServletRequest request, HttpServletResponse response) </span> </li><li class='alt'><span> </span><span class='keyword'>throws</span><span> IOException, ServletException { </span> </li><li><span> String[] infos = request.getPathInfo().split(</span><span class='string'>"/"</span><span>); </span> </li><li class='alt'><span> String name=infos[</span><span class='number'>1</span><span>]; </span> </li><li><span> String id=infos[</span><span class='number'>2</span><span>].substring(</span><span class='number'>0</span><span>, infos[</span><span class='number'>2</span><span>].lastIndexOf(</span><span class='string'>'.'</span><span>)); </span> </li><li class='alt'><span> </span> </li><li><span> OutputStream out = response.getOutputStream(); </span> </li><li class='alt'><span> </span> </li><li><span> ApplicationContext ctx = </span> </li><li class='alt'><span> WebApplicationContextUtils.getWebApplicationContext(getServletContext()); </span> </li><li><span> </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (name.equals(</span><span class='string'>"conAttach"</span><span>)){ </span> </li><li><span> ConAttachsManager mgr = (ConAttachsManager)ctx.getBean(</span><span class='string'>"conAttachsManager"</span><span>); </span> </li><li class='alt'><span> ConAttachs conAttachs = mgr.getConAttachsByContextId(id); </span> </li><li><span> </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span>(conAttachs.getFType().intValue()==</span><span class='number'>1</span><span>){ </span> </li><li><span> </span><span class='keyword'>return</span><span>; </span> </li><li class='alt'><span> } </span> </li><li><span> </span><span class='keyword'>byte</span><span>[] images = conAttachs.getContextFile(); </span> </li><li class='alt'><span> </span><span class='keyword'>try</span><span> { </span> </li><li><span> response.setContentType(conAttachs.getFhttpContent());</span><span class='comment'>//设置contentType </span><span> </span> </li><li class='alt'><span> ImageUtil.outLogo(images,out,</span><span class='number'>200</span><span>,</span><span class='number'>100</span><span>);</span><span class='comment'>//显示200*100的缩略图 </span><span> </span> </li><li><span> response.getOutputStream().flush(); </span> </li><li class='alt'><span> } </span><span class='keyword'>catch</span><span> (Exception e) { </span> </li><li><span> e.printStackTrace(); </span> </li><li class='alt'><span> }</span><span class='keyword'>finally</span><span>{ </span> </li><li><span> </span><span class='keyword'>if</span><span>(out!=</span><span class='keyword'>null</span><span>){ </span> </li><li class='alt'><span> out.close(); </span> </li><li><span> } </span> </li><li class='alt'><span> } </span> </li><li><span> }</span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span>(){ </span> </li><li class='alt'><span> ... ... </span> </li><li><span> } </span> </li><li class='alt'><span> } </span> </li><li><span>} </span> </li></ol></div><p> 2, ImageUtil</p><div class='code_title'>java 代码</div><div class='dp-highlighter'><ol class='dp-j'><li class='alt'><span><span class='keyword'>package</span><span> com.hank.util; </span></span> </li><li><span> </span> </li><li class='alt'><span class='keyword'>import</span><span> ij.ImagePlus; </span> </li><li><span class='keyword'>import</span><span> ij.io.Opener; </span> </li><li class='alt'><span class='keyword'>import</span><span> ij.process.Blitter; </span> </li><li><span class='keyword'>import</span><span> ij.process.ImageProcessor; </span> </li><li class='alt'><span> </span> </li><li><span class='keyword'>import</span><span> java.awt.Graphics; </span> </li><li class='alt'><span class='keyword'>import</span><span> java.awt.Image; </span> </li><li><span class='keyword'>import</span><span> java.awt.image.BufferedImage; </span> </li><li class='alt'><span class='keyword'>import</span><span> java.io.BufferedInputStream; </span> </li><li><span class='keyword'>import</span><span> java.io.BufferedOutputStream; </span> </li><li class='alt'><span class='keyword'>import</span><span> java.io.ByteArrayInputStream; </span> </li><li><span class='keyword'>import</span><span> java.io.ByteArrayOutputStream; </span> </li><li class='alt'><span class='keyword'>import</span><span> java.io.File; </span> </li><li><span class='keyword'>import</span><span> java.io.FileOutputStream; </span> </li><li class='alt'><span class='keyword'>import</span><span> java.io.IOException; </span> </li><li><span class='keyword'>import</span><span> java.io.OutputStream; </span> </li><li class='alt'><span class='keyword'>import</span><span> java.io.PrintWriter; </span> </li><li><span> </span> </li><li class='alt'><span class='keyword'>import</span><span> org.apache.commons.logging.Log; </span> </li><li><span class='keyword'>import</span><span> org.apache.commons.logging.LogFactory; </span> </li><li class='alt'><span> </span> </li><li><span class='keyword'>import</span><span> com.sun.image.codec.jpeg.ImageFormatException; </span> </li><li class='alt'><span class='keyword'>import</span><span> com.sun.image.codec.jpeg.JPEGCodec; </span> </li><li><span class='keyword'>import</span><span> com.sun.image.codec.jpeg.JPEGImageEncoder; </span> </li><li class='alt'><span> </span> </li><li><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * @author hank </span> </span> </li><li><span><span class='comment'> * @date 2007-9-12 03:14:21 </span> </span> </li><li class='alt'><span><span class='comment'> * @version </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span class='keyword'>public</span><span> </span><span class='keyword'>class</span><span> ImageUtil { </span> </li><li><span> </span><span class='keyword'>protected</span><span> </span><span class='keyword'>final</span><span> </span><span class='keyword'>transient</span><span> Log log = LogFactory.getLog(getClass()); </span> </li><li class='alt'><span> </span> </li><li><span> </span><span class='keyword'>public</span><span> </span><span class='keyword'>static</span><span> </span><span class='keyword'>void</span><span> outLogo(</span><span class='keyword'>byte</span><span>[] source,OutputStream out,</span><span class='keyword'>int</span><span> dwidth,</span><span class='keyword'>int</span><span> dheight) </span><span class='keyword'>throws</span><span> Exception{ </span> </li><li class='alt'><span> BufferedInputStream stream = </span><span class='keyword'>new</span><span> BufferedInputStream((</span><span class='keyword'>new</span><span> ByteArrayInputStream(source)),</span><span class='number'>8092</span><span>);</span><span class='comment'>//控制流速 </span><span> </span> </li><li><span> Image src = javax.imageio.ImageIO.read(stream); </span> </li><li class='alt'><span> </span><span class='keyword'>int</span><span> width=src.getWidth(</span><span class='keyword'>null</span><span>); </span> </li><li><span> </span><span class='keyword'>int</span><span> height=src.getHeight(</span><span class='keyword'>null</span><span>); </span> </li><li class='alt'><span> </span><span class='keyword'>int</span><span> towidth,toheight; </span> </li><li><span> </span><span class='keyword'>if</span><span> (width>dwidth || height>dheight){ </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (((</span><span class='keyword'>float</span><span>)width/dwidth)>=((</span><span class='keyword'>float</span><span>)height/dheight)){ </span> </li><li><span> towidth = dwidth; </span> </li><li class='alt'><span> toheight = (height*dwidth)/width; </span> </li><li><span> }</span><span class='keyword'>else</span><span>{ </span> </li><li class='alt'><span> toheight = dheight; </span> </li><li><span> towidth = (width* dheight)/height; </span> </li><li class='alt'><span> } </span> </li><li><span> }</span><span class='keyword'>else</span><span>{ </span> </li><li class='alt'><span> towidth= width; </span> </li><li><span> toheight=height; </span> </li><li class='alt'><span> } </span> </li><li><span> </span> </li><li class='alt'><span> BufferedImage tag = </span><span class='keyword'>new</span><span> BufferedImage(towidth,toheight,BufferedImage.TYPE_INT_RGB); </span> </li><li><span> tag.getGraphics().drawImage(src,</span><span class='number'>0</span><span>,</span><span class='number'>0</span><span>,towidth,toheight,</span><span class='keyword'>null</span><span>); </span> </li><li class='alt'><span> JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); </span> </li><li><span> encoder.encode(tag); </span> </li><li class='alt'><span> } </span> </li><li><span>} </span> </li></ol></div><p> 3 ,在web.xml中配置 servlet</p><p>4 , 页面上直接使用img标签指定src即可 </p><p> </p></div>
1 楼
yourgame
2008-04-05
import ij.ImagePlus;
import ij.io.Opener;
import ij.process.Blitter;
import ij.process.ImageProcessor;
貌似没有使用imageJ
import ij.io.Opener;
import ij.process.Blitter;
import ij.process.ImageProcessor;
貌似没有使用imageJ
相关推荐
本项目"jsp+servlet小项目(实现图片上传功能)"旨在教授如何利用这两者创建一个简单的图片上传功能。下面将详细介绍这个项目涉及的关键知识点。 1. JSP基础:JSP是一种动态网页技术,允许开发者在HTML页面中嵌入...
9. **前端展示**:在`JSP`页面上,可以显示上传结果,比如成功信息、错误信息,甚至预览上传的图片。 以上就是`JSP`和`Servlet`结合实现图片上传功能的基本流程。实践中,还可能涉及到安全性优化,如使用MIME类型...
在JavaWeb开发中,文件上传和显示图片是常见的功能需求,尤其在构建MVC架构的Web应用时。本文将深入探讨如何使用Eclipse IDE,在MVC模式下利用JSP、Servlet、DAO、Service等组件来实现图片的上传、存储以及查询显示...
在IT行业中,动态生成GIF图片的servlet是一种常见的服务器端技术,主要用于创建自定义的图形,例如在网页上显示动态的文本或图像。这里提到的servlet是基于Java编程语言实现的,它利用JavaBean组件来处理图像生成的...
本文将深入探讨如何利用Java技术实现在图片上传后立即在页面上显示。首先,我们需要理解这个过程涉及的关键技术点,包括文件上传、文件处理以及前端展示。 1. **文件上传**:Java中的文件上传通常使用Servlet或第三...
在"使用Servlet生成图片验证码"这个主题中,我们首先会创建一个Servlet来生成随机的字母和数字组合,通常包含4到6位。这可以通过Java的Random类和Character类来实现。验证码生成后,将其保存在服务器的内存中,以便...
7. **JSP页面集成**:在`WebRoot`下的JSP页面中,创建一个链接,其`href`属性指向`servlet`的URL,携带必要的参数,如文件ID。用户点击该链接,浏览器将发起HTTP请求,触发`servlet`执行上述步骤。 综上所述,这个...
通过创建HTML表单,利用Servlet解析和存储上传文件,开发者可以构建自己的文件上传系统。这个示例不仅涵盖了基本的上传流程,还提供了处理上传异常和反馈结果的思路,是学习和实践此类功能的好材料。
在本项目中,我们主要探讨了Java Web开发中的三个核心组件:JSP(JavaServer Pages)、Servlet和JavaBean,以及如何结合它们实现用户登录功能和分页显示操作。此外,项目还利用了Google的验证码服务来增强安全性。 ...
完成图片生成后,Servlet会将其以JPEG或PNG等格式输出到HTTP响应中,客户端接收到图片并在网页上显示。用户需要输入看到的验证码,然后提交到服务器。 在客户端提交验证码后,Servlet会检查提交的字符串是否与之前...
而在下载部分,Servlet则负责根据请求生成含有图片内容的HTTP响应,让客户端能够接收到并显示图片。 要实现图片上传,我们需要以下几个步骤: 1. **创建表单**:在HTML页面中创建一个包含文件输入字段的表单,用户...
在Java Web环境中,我们可以利用Servlet来生成和校验验证码。以下是一个简单的步骤概述: 1. **生成验证码**:首先,我们需要创建一个Servlet来生成验证码。这通常包括以下几个部分: - 生成随机字符串:使用`...
总结来说,这个项目展示了如何在Java Web环境中结合使用JSP和Servlet,通过处理数据和利用图表库生成饼状图,最终以图像形式在网页上展示。这种能力在数据分析、报表系统等场景中非常常见,有助于提升用户对数据的...
在“struts1上传图片,保存数据库,再显示jsp页面”这个场景中,我们将探讨如何利用Struts1实现文件上传,将图片数据保存到数据库,并在JSP页面上展示。 1. **Struts1的文件上传组件**: Struts1并不直接支持文件...
2. 图片转换:将BLOB数据转换回二进制流,然后可以将其设置为HTTP响应的输出流,以下载或在网页上显示图片。 四、Web应用集成 1. JSP/Servlet:在Java Web应用中,Servlet通常用来处理图片的读取和写入,JSP则用于...
在本文中,我们将探讨如何在JSP页面中通过Servlet将服务器硬盘上的图片展示到浏览器上。这是一个相当实用的技术,可用于动态生成图片或从服务器存储中检索图片资源,并将其作为响应发送到用户浏览器。下面将分步骤...
在IT行业中,Servlet和JSP(JavaServer Pages)是Java Web开发中的重要组成部分,它们用于构建动态网页应用程序。本项目“servlet与jsp例子”旨在通过实际操作展示这两种技术的结合使用,以及如何利用反射机制实现...
在本项目"基于Java servlet的网上商品信息查询"中,我们主要关注的是如何利用Java Servlet技术来构建一个能够与数据库交互的在线商品信息查询系统。Servlet是Java Web开发中的核心组件,它允许开发者处理HTTP请求并...
在IT行业中,"jsp+servlet+javabean图形打印器"是一个基于Web的打印解决方案,它利用了Java技术栈中的三个关键组件:JSP(JavaServer Pages)、Servlet和JavaBean。这三个组件共同协作,实现了从Web应用程序中生成和...
7. 渲染HTML页面:如果需要在网页上显示验证码,可以返回一个包含验证码图像的HTML页面。 四、示例代码 ```java import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.Random...