浏览 1505 次
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-11-16
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(); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |