已阅读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咸阳秦都区社区工作者招聘备考题库(230人)及答案详解(各地真题)
- 2025青海省投资集团有限公司招聘134人参考考点题库及答案解析
- 2025福建南平市建瓯市招聘城市社区专职工作人员30人备考题库含答案详解(满分必刷)
- 儿安宁疗护家庭哀伤随访中的隐私保护策略
- 2026福建省面向江南大学选调生选拔工作备考题库附答案详解
- 2025河南郑州高新区招聘社区工作人员100人备考题库及1套参考答案详解
- 2025浙江台州市公安局椒江分局招聘18人备考题库及完整答案详解一套
- 2025年黄山市徽州区消防救援大队政府专职消防员招聘14人备考题库含答案详解(突破训练)
- 2026辽宁大连市沙河口区教育系统招聘应届毕业生104人参考考点题库及答案解析
- 2025内蒙古鄂尔多斯市鄂托克前旗招聘专职社区工作者20人备考题库含答案详解ab卷
- 2025天津大学管理岗位集中招聘15人考试笔试备考题库及答案解析
- 2025年智能制造工厂改造项目可行性研究报告及总结分析
- 动物检疫大比武复习题
- 律师事务所诉讼案件办案进度及当事人满意度绩效评定表
- 《中国法制史》自考考试知识点全覆盖测试题库(附答案)
- 2025年4月全国高等教育自学考试企业管理概论真题及答案解析
- 2025年《互联网内容审核》知识考试题库及答案解析
- 四川省内江市2024-2025学年八年级上学期期末考试数学试题
- 寄卖行合作协议合同
- 油菜高产栽培技术课件
- JJF(闽)1121-2021温度交变、冲击试验设备校准规范-(现行有效)
评论
0/150
提交评论