找回密码
 入住遨海湾
搜索
网站解决方案专享优惠-3折上云
查看: 1179|回复: 0

Servlet上传文件打水印生成缩略图全攻略

[复制链接]
发表于 2007-1-24 13:51:00 | 显示全部楼层 |阅读模式

登录后查才能浏览下载更多咨询,有问题联系QQ:3283999

您需要 登录 才可以下载或查看,没有账号?入住遨海湾

×


package zengxiaomeng;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.ImageIcon;

import com.jspsmart.upload.*;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;

public class UploadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private final static long FILE_MAX_SIZE=4000000;//设置允许上传文件的大小限制
    private final static String FILE_EXTEND_NAME="jpg,JPG,gif,GIF,png,PNG";//设置允许上传文件的扩展名
    private final static String UPLOAD_DIRECTORY="/upload/";//设置上传的路径
    private String path; //上传的绝对路径
    private ServletConfig config;
   
    final public void init(ServletConfig config) throws ServletException {
        this.config = config;
        path=config.getServletContext().getRealPath(UPLOAD_DIRECTORY)+"/";
    }
    final public ServletConfig getServletConfig() {
        return config;
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //PrintWriter out = response.getWriter();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=GBK");
        //PrintWriter out = response.getWriter();

        String filename=upload(config,request,response);
        watermark(filename);//生成水印
        reduce(filename);//生成缩略图
        response.sendRedirect("/Test.htm");
    }
    public void  destroy () {
    }
    //********************************附加的一些自定义方法********************************//
    //上传文件
    private String upload(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //初始化上传组件
        SmartUpload mySmartUpload=new SmartUpload();
        mySmartUpload.initialize(config,request,response);
        mySmartUpload.setAllowedFilesList(FILE_EXTEND_NAME);
        mySmartUpload.setMaxFileSize(FILE_MAX_SIZE);
        //上传文件
        try{
            mySmartUpload.upload();
            //mySmartUpload.save(path);//注释掉此行将用原始文件名来保存
            //以下三行代码利用Calendar对象生成一个随机毫秒数来作为新的文件名保存
            com.jspsmart.upload.File myFile=mySmartUpload.getFiles().getFile(0);//返回刚刚上传的文件对象
            String filename=String.valueOf(Calendar.getInstance().getTimeInMillis())+"."+myFile.getFileExt();//构造文件名
            myFile.saveAs(path+filename,SmartUpload.SAVE_PHYSICAL);//保存文件为新的文件名
            return filename;
        }catch(Exception e){}
        return "0";
    }
    //生成缩略图
    private void reduce(String filename){
        //设置新文件的宽和高
        int tagImageWidth=102;
        int tagImageHeight=120;
       
        ImageIcon srcFile=new ImageIcon(path+filename);//读入源文件
        Image srcImage =srcFile.getImage();//构造Image对象
        BufferedImage tagImage = new BufferedImage(tagImageWidth,tagImageHeight,BufferedImage.TYPE_INT_RGB);//构造图像缓存对象
        tagImage.getGraphics().drawImage(srcImage,0,0,tagImageWidth,tagImageHeight,null);//绘制缩小后的图
        paint(tagImage,"s_"+filename);
    }
    //生成水印
    private void watermark(String filename){
        Color color=Color.RED;//前景色
        Color backgroundColor=Color.WHITE;//背景色
        String word="zengxiaomeng";//水印文字
        String wartmarkfilename="fjsenlogo.jpg";//水印图片地址
        int watermarkLocationX=100;
        int watermarkLocationY=100;
        int wordLocationX=100;
        int wordLocationY=100;
       
        ImageIcon srcFile=new ImageIcon(path+filename);//读入源文件
        Image srcImage =srcFile.getImage();//构造Image对象       
        ImageIcon watermarkFile=new ImageIcon(path+wartmarkfilename);//读入水印文件
        Image watermarkImage =watermarkFile.getImage();//构造水印图片对象       
        BufferedImage tagImage=new BufferedImage(srcImage.getWidth(null),srcImage.getHeight(null), BufferedImage.TYPE_INT_RGB);//构造图像缓存对象
       
        Graphics2D g=tagImage.createGraphics( );
        g.setColor(color);//设置绘图笔的前景色
        g.setBackground(backgroundColor);//设置绘图笔的背景色
        g.drawImage(srcImage, 0, 0, null );//先绘制源图
        g.drawImage(watermarkImage, watermarkLocationX,watermarkLocationY, null ); //添加水印图片
        g.drawString(word,wordLocationX,wordLocationY); //添加文字
        g.dispose();
        paint(tagImage,filename);
    }
    private void paint(BufferedImage tagImage,String tagFilename){
        try{
            FileOutputStream tagFile=new FileOutputStream(path+tagFilename);
            JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(tagFile);
            JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(tagImage);
            param.setQuality(50f, true);
            encoder.encode(tagImage, param);
            tagFile.close();
        }catch(Exception e){}
    }
}

遨海湾-心灵的港湾 www.aosea.com
您需要登录后才可以回帖 登录 | 入住遨海湾

本版积分规则

网站解决方案专享优惠-3折上云

QQ|手机版|小黑屋|遨海湾超级社区

GMT+8, 2024-11-25 06:08

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表