论坛首页 Java企业应用论坛

利用独立的servlet在页面显示图片

浏览 8452 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-09-19  

        web页面上的图片显示可以指定了一个url, 直接访问后台的一个servlet. 在后台数据库可以专门建一个图片库. 以下是一个简单的示例.在这里采用了imageJ-v1.37项目(网址: http://rsb.info.nih.gov/ij/ ) 进行图片处理.

 

1, ImageViewServlet 

java 代码
  1. package com.hank.web.servlet;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.OutputStream;   
  5.   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.http.HttpServlet;   
  8. import javax.servlet.http.HttpServletRequest;   
  9. import javax.servlet.http.HttpServletResponse;   
  10.   
  11. import org.apache.commons.logging.Log;   
  12. import org.apache.commons.logging.LogFactory;   
  13. import org.springframework.context.ApplicationContext;   
  14. import org.springframework.web.context.support.WebApplicationContextUtils;   
  15.   
  16. import com.hank.model.ConAttachs;   
  17. import com.hank.service.ConAttachsManager;   
  18. import com.hank.util.ImageUtil;   
  19.   
  20. /**  
  21.  * @author hank  
  22.  * @date 2007-9-12 03:49:57  
  23.  * @version   
  24.  */  
  25. public class ImageViewServlet extends HttpServlet {   
  26.     private static final long serialVersionUID = 714920135153152003L;   
  27.     protected final transient Log log = LogFactory.getLog(getClass());   
  28.     @Override  
  29.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
  30.         execute(request, response);   
  31.     }   
  32.   
  33.     @Override  
  34.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
  35.         execute(request, response);   
  36.     }   
  37.        
  38.     public void execute(HttpServletRequest request, HttpServletResponse response)   
  39.     throws IOException, ServletException {   
  40.         String[] infos = request.getPathInfo().split("/");   
  41.         String name=infos[1];   
  42.         String id=infos[2].substring(0, infos[2].lastIndexOf('.'));   
  43.            
  44.         OutputStream out = response.getOutputStream();   
  45.            
  46.         ApplicationContext ctx =    
  47.             WebApplicationContextUtils.getWebApplicationContext(getServletContext());   
  48.            
  49.         if (name.equals("conAttach")){   
  50.             ConAttachsManager mgr = (ConAttachsManager)ctx.getBean("conAttachsManager");   
  51.             ConAttachs conAttachs = mgr.getConAttachsByContextId(id);   
  52.                
  53.             if(conAttachs.getFType().intValue()==1){   
  54.                 return;   
  55.             }   
  56.             byte[] images = conAttachs.getContextFile();   
  57.             try {   
  58.                 response.setContentType(conAttachs.getFhttpContent());//设置contentType   
  59.                 ImageUtil.outLogo(images,out,200,100);//显示200*100的缩略图   
  60.                 response.getOutputStream().flush();   
  61.             } catch (Exception e) {   
  62.                 e.printStackTrace();   
  63.             }finally{   
  64.                 if(out!=null){   
  65.                     out.close();   
  66.                 }   
  67.             }   
  68.         }else if(){   
  69.             ... ...   
  70.         }   
  71.     }   
  72. }   

 2, ImageUtil

java 代码
  1. package com.hank.util;   
  2.   
  3. import ij.ImagePlus;   
  4. import ij.io.Opener;   
  5. import ij.process.Blitter;   
  6. import ij.process.ImageProcessor;   
  7.   
  8. import java.awt.Graphics;   
  9. import java.awt.Image;   
  10. import java.awt.image.BufferedImage;   
  11. import java.io.BufferedInputStream;   
  12. import java.io.BufferedOutputStream;   
  13. import java.io.ByteArrayInputStream;   
  14. import java.io.ByteArrayOutputStream;   
  15. import java.io.File;   
  16. import java.io.FileOutputStream;   
  17. import java.io.IOException;   
  18. import java.io.OutputStream;   
  19. import java.io.PrintWriter;   
  20.   
  21. import org.apache.commons.logging.Log;   
  22. import org.apache.commons.logging.LogFactory;   
  23.   
  24. import com.sun.image.codec.jpeg.ImageFormatException;   
  25. import com.sun.image.codec.jpeg.JPEGCodec;   
  26. import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  27.   
  28. /**  
  29.  * @author hank  
  30.  * @date 2007-9-12 03:14:21  
  31.  * @version   
  32.  */  
  33. public class ImageUtil {   
  34.     protected final transient Log log = LogFactory.getLog(getClass());   
  35.        
  36.     public static void outLogo(byte[] source,OutputStream out,int dwidth,int dheight) throws Exception{   
  37.         BufferedInputStream stream = new BufferedInputStream((new ByteArrayInputStream(source)),8092);//控制流速   
  38.         Image src = javax.imageio.ImageIO.read(stream);   
  39.         int width=src.getWidth(null);   
  40.         int height=src.getHeight(null);   
  41.         int towidth,toheight;   
  42.         if (width>dwidth || height>dheight){   
  43.             if (((float)width/dwidth)>=((float)height/dheight)){   
  44.                 towidth = dwidth;   
  45.                 toheight = (height*dwidth)/width;   
  46.             }else{   
  47.                 toheight = dheight;   
  48.                 towidth = (width* dheight)/height;   
  49.             }   
  50.         }else{   
  51.             towidth= width;   
  52.             toheight=height;   
  53.         }   
  54.            
  55.         BufferedImage tag = new BufferedImage(towidth,toheight,BufferedImage.TYPE_INT_RGB);   
  56.         tag.getGraphics().drawImage(src,0,0,towidth,toheight,null);   
  57.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  58.         encoder.encode(tag);   
  59.     }   
  60. }  

 3 ,在web.xml中配置 servlet

4 , 页面上直接使用img标签指定src即可

 

  • ij.jar (1 MB)
  • 描述: imageJ包
  • 下载次数: 270
   发表时间:2008-04-05  
import ij.ImagePlus;   
import ij.io.Opener;   
import ij.process.Blitter;   
import ij.process.ImageProcessor; 

貌似没有使用imageJ 
0 请登录后投票
   发表时间:2008-04-06  
hank 写道

        web页面上的图片显示可以指定了一个url, 直接访问后台的一个servlet. 在后台数据库可以专门建一个图片库. 以下是一个简单的示例.在这里采用了imageJ-v1.37项目(网址: http://rsb.info.nih.gov/ij/ ) 进行图片处理.

 

1, ImageViewServlet 

java 代码
  1. package com.hank.web.servlet;   
  2.   
  3. import java.io.IOException;   
  4. testtest 
  5. import javax.servlet.http.HttpServletRequest;   
  6. import javax.servlet.http.HttpServletResponse;   
  7.   
  8. import org.apache.commons.logging.Log;   
  9. import org.apache.commons.logging.LogFactory;   
  10. import org.springframework.context.ApplicationContext;   
  11. import org.springframework.web.context.support.WebApplicationContextUtils;   
  12.   
  13. import com.hank.model.ConAttachs;   
  14. import com.hank.service.ConAttachsManager;   
  15. import com.hank.util.ImageUtil;   
  16.   
  17. /**  
  18.  * @author hank  
  19.  * @date 2007-9-12 03:49:57  
  20.  * @version   
  21.  */  
  22. public class ImageViewServlet extends HttpServlet {   
  23.     private static final long serialVersionUID = 714920135153152003L;   
  24.     protected final transient Log log = LogFactory.getLog(getClass());   
  25.     @Override  
  26.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
  27.         execute(request, response);   
  28.     }   
  29.   
  30.     @Override  
  31.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
  32.         execute(request, response);   
  33.     }   
  34.        
  35.     public void execute(HttpServletRequest request, HttpServletResponse response)   
  36.     throws IOException, ServletException {   
  37.         String[] infos = request.getPathInfo().split("/");   
  38.         String name=infos[1];   
  39.         String id=infos[2].substring(0, infos[2].lastIndexOf('.'));   
  40.            
  41.         OutputStream out = response.getOutputStream();   
  42.            
  43.         ApplicationContext ctx =    
  44.             WebApplicationContextUtils.getWebApplicationContext(getServletContext());   
  45.            
  46.         if (name.equals("conAttach")){   
  47.             ConAttachsManager mgr = (ConAttachsManager)ctx.getBean("conAttachsManager");   
  48.             ConAttachs conAttachs = mgr.getConAttachsByContextId(id);   
  49.                
  50.             if(conAttachs.getFType().intValue()==1){   
  51.                 return;   
  52.             }   
  53.             byte[] images = conAttachs.getContextFile();   
  54.             try {   
  55.                 response.setContentType(conAttachs.getFhttpContent());//设置contentType   
  56.                 ImageUtil.outLogo(images,out,200,100);//显示200*100的缩略图   
  57.                 response.getOutputStream().flush();   
  58.             } catch (Exception e) {   
  59.                 e.printStackTrace();   
  60.             }finally{   
  61.                 if(out!=null){   
  62.                     out.close();   
  63.                 }   
  64.             }   
  65.         }else if(){   
  66.             ... ...   
  67.         }   
  68.     }   
  69. }   

 2, ImageUtil

java 代码
  1. package com.hank.util;   
  2.   
  3. import ij.ImagePlus;   
  4. import ij.io.Opener;   
  5. import ij.process.Blitter;   
  6. import ij.process.ImageProcessor;   
  7.   
  8. import java.awt.Graphics;   
  9. import java.awt.Image;   
  10. import java.awt.image.BufferedImage;   
  11. import java.io.BufferedInputStream;   
  12. import java.io.BufferedOutputStream;   
  13. import java.io.ByteArrayInputStream;   
  14. import java.io.ByteArrayOutputStream;   
  15. import java.io.File;   
  16. import java.io.FileOutputStream;   
  17. import java.io.IOException;   
  18. import java.io.OutputStream;   
  19. import java.io.PrintWriter;   
  20.   
  21. import org.apache.commons.logging.Log;   
  22. import org.apache.commons.logging.LogFactory;   
  23.   
  24. import com.sun.image.codec.jpeg.ImageFormatException;   
  25. import com.sun.image.codec.jpeg.JPEGCodec;   
  26. import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  27.   
  28. /**  
  29.  * @author hank  
  30.  * @date 2007-9-12 03:14:21  
  31.  * @version   
  32.  */  
  33. public class ImageUtil {   
  34.     protected final transient Log log = LogFactory.getLog(getClass());   
  35.        
  36.     public static void outLogo(byte[] source,OutputStream out,int dwidth,int dheight) throws Exception{   
  37.         BufferedInputStream stream = new BufferedInputStream((new ByteArrayInputStream(source)),8092);//控制流速   
  38.         Image src = javax.imageio.ImageIO.read(stream);   
  39.         int width=src.getWidth(null);   
  40.         int height=src.getHeight(null);   
  41.         int towidth,toheight;   
  42.         if (width>dwidth || height>dheight){   
  43.             if (((float)width/dwidth)>=((float)height/dheight)){   
  44.                 towidth = dwidth;   
  45.                 toheight = (height*dwidth)/width;   
  46.             }else{   
  47.                 toheight = dheight;   
  48.                 towidth = (width* dheight)/height;   
  49.             }   
  50.         }else{   
  51.             towidth= width;   
  52.             toheight=height;   
  53.         }   
  54.            
  55.         BufferedImage tag = new BufferedImage(towidth,toheight,BufferedImage.TYPE_INT_RGB);   
  56.         tag.getGraphics().drawImage(src,0,0,towidth,toheight,null);   
  57.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  58.         encoder.encode(tag);   
  59.     }   
  60. }  

 3 ,在web.xml中配置 servlet

4 , 页面上直接使用img标签指定src即可

 

0 请登录后投票
   发表时间:2008-04-08  
这样生成的缩略图貌似很粗糙的。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics