Struts1.X上传图片后加上水印

   相信熟悉struts的朋友对利用struts框架上传文件都非常熟悉了,不过自己在以前有过上传文件后然后加上水印的需求,这次发上来与大家分享一下,希望能给有需要的人一点帮助,高手就无视啦……

 这里只发上业务处理部分,视图部分就简单一个jsp页面

FileUploadAction.java
  1. ........
  2. public ActionForward execute(ActionMapping mapping, ActionForm form,
  3.             HttpServletRequest request, HttpServletResponse response) {
  4.         //拿到文件表单
  5.         FileForm fileForm = (FileForm) form;
  6.         String name=fileForm.getFile().getFileName();//得到文件名
  7.         String realpath=this.getServlet().getServletContext().getRealPath("/")+name;//创建文件的路径名
  8.         
  9.         try {
  10.             //拿到文件输入流
  11.             InputStream in=fileForm.getFile().getInputStream();
  12.             byte[] buf=new byte[1024*2];
  13.             int length=0;
  14.             //文件输出流
  15.             FileOutputStream out=new FileOutputStream(realpath);
  16.             while((length=in.read(buf))!=-1){
  17.                 out.write(buf,0,length);//读进缓冲区,再写入到文件中
  18.             }
  19.             in.close();
  20.             out.close();
  21.             System.out.println("文件上传成功!");
  22.             //用上传成功的图片构件一个流
  23.             FileInputStream ino=new FileInputStream(realpath); 
  24.             //将流变为图片
  25.             BufferedImage image=ImageIO.read(ino);
  26.             /*在图片上做手脚(即打水印,说话粗俗了点,呵呵)*/
  27.             Graphics g=image.getGraphics();//拿到图片画笔
  28.             g.setFont(new Font("华文新魏",Font.BOLD,35));
  29.             //水印打的位置
  30.             g.drawString("IvanZhang", image.getWidth()-350,image.getHeight()-35);
  31.             //手脚做好以后,放回原来的位置
  32.             FileOutputStream out2=new FileOutputStream(realpath);           
  33.             JPEGImageEncoder enc=JPEGCodec.createJPEGEncoder(out2);//换成JPEG Data streams. 
  34.             enc.encode(image);//Encode a BufferedImage as a JPEG data stream.
  35.             //把门关上,神不知鬼不觉
  36.             ino.close();
  37.             out2.close();
  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         } 
  41.         return null;
  42.     }
  43. ..........
    
表单类:
FileForm.java
  1. public class FileForm extends ActionForm {
  2.     /** file property */
  3.     private FormFile file;
  4.     /** 
  5.      * Method validate
  6.      * @param mapping
  7.      * @param request
  8.      * @return ActionErrors
  9.      */
  10.     public ActionErrors validate(ActionMapping mapping,
  11.             HttpServletRequest request) {
  12.         // TODO Auto-generated method stub
  13.         return null;
  14.     }
  15.     /** 
  16.      * Method reset
  17.      * @param mapping
  18.      * @param request
  19.      */
  20.     public void reset(ActionMapping mapping, HttpServletRequest request) {
  21.         // TODO Auto-generated method stub
  22.     }
  23.     /** 
  24.      * Returns the file.
  25.      * @return String
  26.      */
  27.     public FormFile getFile() {
  28.         return file;
  29.     }
  30.     /** 
  31.      * Set the file.
  32.      * @param file The file to set
  33.      */
  34.     public void setFile(FormFile file) {
  35.         this.file = file;
  36.     }
  37. }

    简单的功能实现,大家不仿试试看,
    水印部分用到了 com.sun.image.codec.jpeg包中的内容,有兴趣的朋友可自己查阅相关API
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值