`
liyanboss
  • 浏览: 140078 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

如何把客户端上传的图片 先 加上水印再保存到服务端??????

阅读更多
最近对图片加上水印,有兴趣,找到一个牛人写的帖子,转载到这里,供大家分享!!!!!!


package   net.csdn.xdj.model.d050403;  
   
  import   java.io.*;  
  import   java.awt.*;  
  import   java.awt.image.*;  
  import   com.sun.image.codec.jpeg.*;  
  /**  
    *   <p>加入水印信息</p>  
    *   <p>Title:   community.csnd.net</p>  
    *   <p>Description:   java问题解答</p>  
    *   <p>Copyright:   Copyright   (c)   2005</p>  
    *   <p>Company:   自由人</p>  
    *   @author   许德建(simonxuluo)  
    *   @version   1.0  
    */  
  public   class   Mark   {  
      public   Mark()   {  
      }  
   
      public   static   ByteArrayOutputStream   manipulateImage(String   message,  
              byte[]   imageData)   {  
          ByteArrayOutputStream   baos   =   null;  
          Frame   frame   =   null;  
          Graphics   graphics   =   null;  
          try   {  
              frame   =   new   Frame();  
              frame.addNotify();  
              MediaTracker   mt   =   new   MediaTracker(frame);  
              Image   image   =   Toolkit.getDefaultToolkit().createImage(imageData);  
              mt.addImage(image,   0);  
              mt.waitForAll();  
              int   w   =   image.getWidth(frame);  
              int   h   =   image.getHeight(frame);  
              BufferedImage   offscreen   =   new   BufferedImage(w,   h,  
                                                                                                      BufferedImage.TYPE_3BYTE_BGR);  
              graphics   =   offscreen.getGraphics();  
              graphics.drawImage(image,   0,   0,   frame);  
              graphics.setColor(Color.RED);  
              graphics.setFont(new   Font("宋体",   Font.BOLD   |   Font.ITALIC,   20));  
              graphics.drawString(message,   9,   29);  
              graphics.setColor(Color.WHITE);  
              graphics.drawString(message,   10,   30);  
              baos   =   new   ByteArrayOutputStream();  
              JPEGImageEncoder   encoder   =   com.sun.image.codec.jpeg.JPEGCodec.  
                      createJPEGEncoder(baos);  
              encoder.encode(offscreen);  
          }  
          catch   (InterruptedException   e)   {  
              e.printStackTrace(System.out);   //To   change   body   of   catch   statement   use   File   |   Settings   |   File   Templates.  
          }  
          catch   (IOException   e)   {  
              e.printStackTrace(System.out);   //To   change   body   of   catch   statement   use   File   |   Settings   |   File   Templates.  
          }  
          finally   {  
              if   (graphics   !=   null)   {  
                  graphics.dispose();  
              }  
              if   (frame   !=   null)   {  
                  frame.removeNotify();  
              }  
          }  
          return   baos;  
      }  
  }  










package   net.csdn.xdj.servlet.d050403;  
   
  import   org.apache.struts.action.*;  
  import   javax.servlet.http.HttpServletRequest;  
  import   org.apache.struts.upload.FormFile;  
   
  /**  
    *   <p>页面表单影射,Action   FormBean</p>  
    *   <p>Title:   community.csnd.net</p>  
    *   <p>Description:   java问题解答</p>  
    *   <p>Copyright:   Copyright   (c)   2005</p>  
    *   <p>Company:   自由人</p>  
    *   @author   许德建(simonxuluo)  
    *   @version   1.0  
    */  
  public   class   UploadAFB  
          extends   ActionForm   {  
      private   String   theAction;  
      private   FormFile   theFile;  
      private   String   newFile;  
   
      public   void   setTheAction(String   theAction)   {  
          this.theAction   =   theAction;  
      }  
   
      public   String   getTheAction()   {  
          return   this.theAction;  
      }  
   
      public   void   setTheFile(FormFile   theFile)   {  
          this.theFile   =   theFile;  
      }  
   
      public   void   setNewFile(String   newFile)   {  
          this.newFile   =   newFile;  
      }  
   
      public   FormFile   getTheFile()   {  
          return   this.theFile;  
      }  
   
      public   String   getNewFile()   {  
          return   newFile;  
      }  
   
      public   ActionErrors   validate(ActionMapping   mapping,  
                                                                HttpServletRequest   request)   {  
          ActionErrors   errors   =   new   ActionErrors();  
          if   (theAction   !=   null)   {  
              if   (theFile   ==   null)   {  
                  errors.add(errors.GLOBAL_ERROR,   new   ActionError("error",   "没有指定上传文件"));  
              }  
              if   (newFile   ==   null   &   newFile.length()   ==   0)   {  
                  errors.add(errors.GLOBAL_ERROR,   new   ActionError("error",   "没有指定保存文件路径"));  
              }  
          }  
          //   verify  
          return   errors;  
      }  
   
  }  










package   net.csdn.xdj.servlet.d050403;  
   
  import   org.apache.struts.action.*;  
  import   javax.servlet.http.*;  
  import   org.apache.struts.upload.FormFile;  
  import   java.io.*;  
  import   net.csdn.xdj.model.d050403.Mark;  
   
  /**  
    *   <p>上传处理,Action</p>  
    *   <p>Title:   community.csnd.net</p>  
    *   <p>Description:   java问题解答</p>  
    *   <p>Copyright:   Copyright   (c)   2005</p>  
    *   <p>Company:   自由人</p>  
    *   @author   许德建(simonxuluo)  
    *   @version   1.0  
    */  
  public   class   UploadImage  
          extends   Action   {  
      public   ActionForward   execute(ActionMapping   mapping,  
                                                                ActionForm   actionForm,  
                                                                HttpServletRequest   request,  
                                                                HttpServletResponse   response)   {  
          UploadAFB   form   =   (UploadAFB)   actionForm;  
          String   theAction   =   form.getTheAction();  
          form.setTheAction("uploadImage");  
          if   (theAction   ==   null)   {  
              theAction   =   "";  
          }  
          else   if   (theAction.equals("uploadImage"))   {  
              FormFile   file   =   form.getTheFile();  
              File   newFile   =   new   File(form.getNewFile());  
              if   (file   ==   null   ||   newFile   ==   null)   {  
                  return   mapping.getInputForward();  
              }  
              try   {  
                  ByteArrayOutputStream   baos   =   Mark.manipulateImage("许德建",  
                          file.getFileData());  
                  FileOutputStream   fos   =   new   FileOutputStream(newFile);  
                  baos.writeTo(fos);  
                  fos.close();  
                  baos.close();  
                  request.setAttribute("message",   "上传成功");  
                  return   mapping.findForward("response");  
              }  
              catch   (FileNotFoundException   ex)   {  
                  //   error   handle  
              }  
              catch   (IOException   ex)   {  
                  //   error   handle  
              }  
          }  
          return   mapping.getInputForward();  
      }  
  }  
 



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics