




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
转载struts2上传图片实例原文地址:struts2上传图片实例作者:酷云1、页面代码代码:图片上传function upload()var ff = document.forms.imageForm;var img = ff.file.value;if(img=null | img=)alert(图片路径不允许为空!);return;ff.target=_top;ff.method=post;ff.submit();-上传图片:-2、action代码代码:import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Date;import javax.imageio.ImageIO;import mons.logging.Log;import mons.logging.LogFactory;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ImageAction extends ActionSupport private static final long serialVersionUID = -8982586906724587883L;private Log log = LogFactory.getLog(getClass();private static final int BUFFER_SIZE = 16 * 1024;private static final String TEXT_TITLE = 文字水印;private static final String WATER_IMG_NAME = rzx.gif;/ 输入参数:上传过来的文件路径private File file;private String fileName;private String contentType;/ 输出参数private String imageFileName;/ 输出参数:原图保存路径private String ipath;/ 输出参数:缩略图保存路径private String imgPath;/ 输出参数private String json;public ImageAction() Overridepublic String execute() throws Exception return uploadImage();private String getExtention(String fileName) int pos = fileName.lastIndexOf(.);return fileName.substring(pos);private void copy(File src, File dist) throws Exception log.debug(src- + src);log.debug(dist- + dist);try InputStream in = null;OutputStream out = null;try in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);out = new BufferedOutputStream(new FileOutputStream(dist), BUFFER_SIZE);byte buf = new byteBUFFER_SIZE;while (in.read(buf) 0) out.write(buf);out.close();in.close(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); finally if (in != null)in.close();if (out != null)out.close(); catch (Exception e) e.printStackTrace();throw new Exception(e);public String uploadImage() throws Exception log.debug(file- + file);log.debug(file name- + fileName);imageFileName = new Date().getTime() + getExtention(fileName);/ 得到文件存放路径log.debug(imageFileName- + imageFileName);String dir = ServletActionContext.getServletContext().getRealPath(/UploadImages);File dirs = new File(dir);if (!dirs.exists()dirs.mkdir();/ 使用原来的文件名保存图片String path = dir + / + fileName;File imageFile = new File(path);copy(file, imageFile);/ 缩放zoom(imageFile);/ 给大图添加文字水印/ watermark(imageFile);/ 给大图添加图片水印,可以是gif或png格式imageWaterMark(imageFile);/ 创建子目录 得到添加水印后的图片的存储路径,子目录只能一级一级的建String dist = dir + /water;File outFile = new File(dist);if (!outFile.exists()outFile.mkdir();File sImgpath = new File(dist + / + fileName);/ 给小图添加文字水印/ watermark(sImgpath);/ 给小图添加图片水印,可以是gif或png格式imageWaterMark(sImgpath);/ 大图路径ipath = UploadImages/ + fileName;/ 小图路径imgPath = UploadImages/water/ + fileName;return SUCCESS;public void zoom(File imageFile) throws Exception log.debug(zoomimageFile- + imageFile);try / 缩略图存放路径File todir = new File(ServletActionContext.getServletContext().getRealPath(/UploadImages) + /water);if (!todir.exists() todir.mkdir();if (!imageFile.isFile()throw new Exception(imageFile + is not image file error in CreateThumbnail!);File sImg = new File(todir, fileName);BufferedImage Bi = ImageIO.read(imageFile);/ 假设图片宽 高 最大为130 80,使用默认缩略算法Image Itemp = Bi.getScaledInstance(130, 80, Bi.SCALE_DEFAULT);double Ratio = 0.0;if (Bi.getHeight() 130) | (Bi.getWidth() 80) if (Bi.getHeight() Bi.getWidth()Ratio = 80.0 / Bi.getHeight();elseRatio = 130.0 / Bi.getWidth();AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);Itemp = op.filter(Bi, null);ImageIO.write(BufferedImage) Itemp, jpg, sImg); catch (IOException e) e.printStackTrace();throw new Exception(e);public void watermark(File img) throws Exception log.debug(watermark file name- + img.getPath();try if (!img.exists() throw new IllegalArgumentException(file not found!);log.debug(watermarkimg- + img);/ 创建一个FileInputStream对象从源图片获取数据流FileInputStream sFile = new FileInputStream(img);/ 创建一个Image对象并以源图片数据流填充Image src = ImageIO.read(sFile);/ 得到源图宽int width = src.getWidth(null);/ 得到源图长int height = src.getHeight(null);/ 创建一个BufferedImage来作为图像操作容器BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);/ 创建一个绘图环境来进行绘制图象Graphics2D g = image.createGraphics();/ 将原图像数据流载入这个BufferedImagelog.debug(width: + width + height: + height);g.drawImage(src, 0, 0, width, height, null);/ 设定文本字体g.setFont(new Font(宋体, Font.BOLD, 28);String rand = TEXT_TITLE;/ 设定文本颜色g.setColor(Color.blue);/ 设置透明度g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f);/ 向BufferedImage写入文本字符,水印在图片上的坐标g.drawString(rand, width - (width - 20), height - (height - 60);/ 使更改生效g.dispose();/ 创建输出文件流FileOutputStream outi = new FileOutputStream(img);/ 创建JPEG编码对象JPEGImageEncoder encodera = JPEGCodec.createJPEGEncoder(outi);/ 对这个BufferedImage (image)进行JPEG编码encodera.encode(image);/ 关闭输出文件流outi.close();sFile.close(); catch (IOException e) e.printStackTrace();throw new Exception(e);public void imageWaterMark(File imgFile) throws Exception try / 目标文件Image src = ImageIO.read(imgFile);int wideth = src.getWidth(null);int height = src.getHeight(null);BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = image.createGraphics();g.drawImage(src, 0, 0, wideth, height, null);/ 水印文件 路径String waterImgPath = ServletActionContext.getServletContext().getRealPath(/UploadImages)+/+WATER_IMG_NAME;File waterFile = new File(waterImgPath);Image waterImg = ImageIO.read(waterFile);int w_wideth = waterImg.getWidth(null);int w_height = waterImg.getHeight(null);g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f);g.drawImage(waterImg, (wideth - w_wideth) / 2, (height - w_height) / 2, w_wideth, w_height, null);/ 水印文件结束g.dispose();FileOutputStream out = new File
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 环境保护施工技术方案
- 水利水库枢纽工程地质勘察与评估方案
- 2025年饭店管理与服务专业研究生入学考试卷及答案
- 职业教育教师素养的构成与发展路径分析
- 高校会计专业教育改革的策略与探索
- 大数据技术在电子商务课程中的应用研究
- 家庭农场策划咨询方案
- 皮卡车雨季三防应急预案
- 奉节网络营销设计方案
- 湛雪与配偶离婚协议中子女抚养费及财产分配协议
- 中医(中西医结合科)工作制度与岗位职责
- 网络安全课件下载
- 住院精神疾病患者自杀风险护理
- 三年级美术上册全册教案(湘教版)
- 同等学力英语申硕考试词汇(第六版大纲)电子版
- 光伏项目投标方案(技术方案)
- GB/T 44395-2024激光雷达测风数据可靠性评价技术规范
- 公厕保洁服务投标方案
- 模块化炼化设备的设计与集成
- JJF(皖) 174-2024 重点用能单位能源资源计量在线审查规范
- 儿童口腔健康宣教课件
评论
0/150
提交评论