当前位置:文档之家› jsp 图片上传功能实现原创

jsp 图片上传功能实现原创

package com.lsl.util;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;/**** @author Administrator* 图像处理类*/public class PicCompression {/*** 压缩图片方法** @param oldFile* 将要压缩的图片的绝对地址* @param width* 压缩宽* @param height* 压缩长* @param quality* 压缩清晰度<b>建议为1.0</b>* @param smallIcon* 压缩图片后,添加的扩展名* @return*/public String zoom(String oldFile, int width, int height, float quality) {if (oldFile == null) {return null;}String newImage = null;try {File file = new File(oldFile);if(!file.exists()) //文件不存在时return null;/** 对服务器上的临时文件进行处理*/Image srcFile = ImageIO.read(file);// 为等比缩放计算输出的图片宽度及高度double rate1 = ((double) srcFile.getWidth(null)) / (double) width+ 0.1;double rate2 = ((double) srcFile.getHeight(null)) / (double) height+ 0.1;double rate = rate1 > rate2 ? rate1 : rate2;int new_w = (int) (((double) srcFile.getWidth(null)) / rate);int new_h = (int) (((double) srcFile.getHeight(null)) / rate);/** 宽,高设定*/BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB);tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null);String filePrex = oldFile.substring(0, stIndexOf('.'));/** 压缩后的文件名*/// newImage =smallIcon + filePrex// +oldFile.substring(filePrex.length());newImage = filePrex+width+"x"+height + oldFile.substring(filePrex.length());// newImage = smallIcon;/** 压缩之后临时存放位置*/FileOutputStream out = new FileOutputStream(newImage);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);/** 压缩质量*/jep.setQuality(quality, true);encoder.encode(tag, jep);out.close();srcFile.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return newImage;}}package com.lsl.goods.action;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.annotation.Resource;import mons.io.FileUtils;import org.apache.struts2.ServletActionContext;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.lsl.bean.GoodsInfo;import com.lsl.bean.Lsluser;import com.lsl.service.GoodsInfoService;import com.lsl.util.PicCompression;import com.opensymphony.xwork2.ActionContext;@Controller @Scope("prototype")public class SetGoodsPicAction {@Resource GoodsInfoService goodsInfoService;private File image; // 上传的文件如果多文件就用数组形式File[] imageprivate String imageFileName; // 上传的文件名如果多文件就用数组形式String[] imageFileName 这里用来获取文件的后缀名private String imageContentType; //上传文件的类系如果多文件就用数组形式String[] imageContentType//注意文件的大小不再这里设置在struts.xml配置上传文件的大小public File getImage() {return image;}public void setImage(File image) {this.image = image;}public String getImageFileName() {return imageFileName;}public void setImageFileName(String imageFileName) {this.imageFileName = imageFileName;}public String getImageContentType() {return imageContentType;}public void setImageContentType(String imageContentType) {this.imageContentType = imageContentType;}//设置图片public String setGoodsPic() throws IOException{if(image !=null){if(imageContentType.indexOf("image") != -1){ //检测是否为图片String path="/Images/goodsImgs/";String imageBasePath = ServletActionContext.getServletContext().getRealPath(path);Lsluser lu= (Lsluser)ActionContext.getContext().getSession().get("currentUser");String privatePath =lu.getLslId().toString();String realPath=imageBasePath+"\\"+privatePath; //硬盘的目录这里第一个\是转译符意思是第二个\是有效字符SimpleDateFormat dateFormat =new SimpleDateFormat("yyyyMMddHHmmssSSS");String imageName=dateFormat.format(new Date()); //设置文件名以时间命名StringsuffixName=imageFileName.substring(stIndexOf(".")).toLowerCase();//获取文件的后缀名File saveFile= new File(new File(realPath),imageName+suffixName);if(!saveFile.getParentFile().exists()) saveFile.getParentFile().mkdirs();//如果不存在绝对目录那就创建改目录FileUtils.copyFile(image, saveFile); //保存原图成功PicCompression pc = new PicCompression(); //处理图片的类int w=200;int h=250;pc.zoom(newFile(realPath).toString()+"\\"+imageName+suffixName,w,h,1); //生成缩略图命名规则就是在原图的名字后面加上宽度x长度String bigPicRelativePath = path + privatePath+"/"+imageName+suffixName; //实图的相对路径用于存放进数据库String smallPicRelativePath = path + privatePath+"/"+imageName+w+"x"+h+suffixName; //缩略图的相对路径用于存放进数据库GoodsInfoaddingGoods=(GoodsInfo)ActionContext.getContext().getSession().get("addingGoods");addingGoods.setImgUrl(bigPicRelativePath);addingGoods.setSmallImgUrl(smallPicRelativePath);goodsInfoService.update(addingGoods);ActionContext.getContext().getSession().put("setPicRestul", "图片上传成功");}}return "setPicSuccess";}}AnyQuestion --- Qq:16895920。

相关主题