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即可