posts - 309, comments - 6940, trackbacks - 0, articles - 3
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

动态修改图片大小的java servlet源码

Posted on 2007-12-18 16:20 诗特林 阅读(3701) 评论(1)  编辑  收藏 所属分类: Java

                                                        动态修改图片大小的java servlet

代码虽然比较简单,但做个标记,以便是后查找.

1.修改图片的java Servlet代码如下:

package com.photo.gallery;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageResizer extends HttpServlet {
    
private static final long serialVersionUID = -8285774993751841288L;

    
public void doGet(HttpServletRequest request, HttpServletResponse response) {
        String imageOutput 
= getParam(request, "output""png");
        String imageRoot 
= getParam(request, "root""/albums");
        String imageFile 
= getParam(request, "file""/Album1/image1.jpg");
        
int width = Integer.parseInt(getParam(request, "width""250"));
        
int height = Integer.parseInt(getParam(request, "width""0"));
        
if ("png".equals(imageOutput))
            response.setContentType(
"image/png");
        
else
            response.setContentType(
"image/jpeg");
        String imageLoc 
= request.getSession().getServletContext().getRealPath(
                imageRoot)
                
+ imageFile;
        
try {
            BufferedImage bufferedImage 
= ImageIO.read(new File(imageLoc));
            
int calcHeight = height > 0 ? height : (width
                    
* bufferedImage.getHeight() / bufferedImage.getWidth());

            ImageIO.write(createResizedCopy(bufferedImage, width, calcHeight),
                    imageOutput, response.getOutputStream());
        }
 catch (Exception e) {
            log(
"Problem with image: " + imageLoc + e);
        }

    }


    BufferedImage createResizedCopy(Image originalImage, 
int scaledWidth,
            
int scaledHeight) {
        BufferedImage scaledBI 
= new BufferedImage(scaledWidth, scaledHeight,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g 
= scaledBI.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(originalImage, 
00, scaledWidth, scaledHeight, null);
        g.dispose();
        
return scaledBI;
    }


    
private String getParam(HttpServletRequest request, String param, String def) {
        String parameter 
= request.getParameter(param);
        
if (parameter == null || "".equals(parameter)) {
            
return def;
        }
 else {
            
return parameter;
        }

    }

}

2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
    
<servlet>
        
<servlet-name>image</servlet-name>
        
<servlet-class>com.photo.gallery.ImageResizer</servlet-class>
        
<load-on-startup>1</load-on-startup>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>image</servlet-name>
        
<url-pattern>/image</url-pattern>
    
</servlet-mapping>

</web-app>

3.在地址栏输出如下的连接:
http://localhost:8088/Javaimageresizer/image?root=/albums&file=/thumbs/imagename.jpg&width=270&height=100
即可查看效果

评论

# re: 动态修改图片大小的java servlet源码  回复  更多评论   

2009-03-12 21:45 by hanfeng
Servlet也是一种JAVA程序是吧?

只有注册用户登录后才能发表评论。


网站导航: