ImageUpload.java
资源名称:shihua.rar [点击查看]
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:6k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
JavaScript
- package com.chinacannel.common;
- import java.io.*;
- import org.apache.struts.upload.*;
- import com.chinacannel.entity.Image;
- import java.awt.image.BufferedImage;
- import javax.imageio.ImageIO;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import java.util.Date;
- import org.apache.commons.logging.LogFactory;
- import org.apache.commons.logging.Log;
- import javax.servlet.ServletRequest;
- public class ImageUpload {
- private static Log log = LogFactory.getLog(ImageUpload.class);
- public ImageUpload() {
- }
- public Image saveImage(FormFile file, ServletRequest servletRequest) {
- return this.saveImage(file, servletRequest, true);
- }
- public Image saveImage(FormFile file,ServletRequest servletRequest,boolean createThumbnail) {
- if(file==null){
- return null;
- }
- InputStream stream = null;
- OutputStream bos = null;
- try {
- stream = file.getInputStream(); //把文件读入
- String path = servletRequest.getRealPath("/"); //取当前系统路径
- // String filePath = path + file.getFileName();
- path += Params.getImgPath();
- String fileName = file.getFileName();
- Date d = new Date();
- String nFileName = String.valueOf(d.getTime()) + fileName;
- bos = new FileOutputStream(path + nFileName, true); //建立一个上传文件的输出流
- log.info("-------saveImage--------->"+path+nFileName);
- int bytesRead = 0;
- byte[] buffer = new byte[8192];
- while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
- bos.write(buffer, 0, bytesRead); //将文件写入服务器
- }
- String breFileName = "";
- if(createThumbnail){
- breFileName = this.CreateThumbnail(nFileName, servletRequest);
- }
- return this.saveToDB(fileName, nFileName, Params.getImgPath(), breFileName,
- Params.getThumbnailPath());
- } catch (FileNotFoundException e) {
- log.error(e.getMessage(), e);
- return null;
- } catch (Exception ex) {
- log.error(ex.getMessage(), ex);
- return null;
- } finally {
- try {
- bos.close();
- stream.close();
- } catch (Exception ex1) {
- log.error(ex1.getMessage(), ex1);
- }
- }
- }
- protected String CreateThumbnail(String fileName,ServletRequest servletRequest) throws
- Exception {
- String path = servletRequest.getRealPath("/")+Params.getImgPath();
- log.info("------CreateThumbnail---------->"+path);
- File f = new File(path + fileName);
- BufferedImage bi = null;
- try {
- bi = ImageIO.read(f);
- } catch (IOException ex2) {
- throw new IOException(ex2.getMessage());
- }
- String fn = "";
- FileOutputStream out = null;
- String[] s = this.splitFileName(fileName);
- String name = s[0];
- String ext = s[1];
- if ("jpg".equals(ext) || "jpeg".equals(ext)) {
- fn = name + "Bre." + ext;
- } else {
- fn = name + "Bre.png";
- }
- int nw = bi.getWidth() / 2;
- int nh = bi.getHeight() / 2;
- BufferedImage tag = new BufferedImage(nw, nh,
- BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(bi, 0, 0, nw, nh, null); //绘制缩小后的图
- try {
- out = new FileOutputStream(servletRequest.getRealPath("/")+Params.getThumbnailPath() + fn); //输出到文件流
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag); //近JPEG编码
- out.close();
- } catch(Exception ex){
- log.error(ex.getMessage(), ex);
- }
- finally {
- out.close();
- }
- return fn;
- }
- protected String[] splitFileName(String fileName) {
- int a = fileName.lastIndexOf(".");
- String[] r = new String[2];
- r[0] = fileName.substring(0, a);
- r[1] = fileName.substring(a + 1);
- return r;
- }
- protected Image saveToDB(String fileName, String reallyName, String path,
- String ThumbnailName, String ThumbnailPath) {
- CommonDAO dao = new CommonDAO();
- Image img = new Image();
- img.setFilename(fileName);
- img.setReallyname(reallyName);
- img.setPath(path);
- img.setThumbnailname(ThumbnailName);
- img.setThumbnailpath(ThumbnailPath);
- try {
- dao.createObject(img);
- return img;
- } catch (DAOException ex) {
- log.error(ex.getMessage(), ex);
- return null;
- }
- }
- public void deleteImage(int imgId){
- CommonDAO dao = new CommonDAO();
- Image image=null;
- try {
- image=(Image)dao.loadObject(new Long(imgId), Image.class);
- dao.removeObject(image);
- this.deleteFile(image);
- } catch (DAOException ex) {
- log.error(ex.getMessage(), ex);
- }
- }
- protected void deleteFile(Image img){
- if(img==null){
- return;
- }
- try {
- String imgName = img.getPath() + img.getFilename();
- String imgThumbnailName = img.getThumbnailpath() +
- img.getThumbnailname();
- File file = new File(imgName);
- File thumbnailFile = new File(imgThumbnailName);
- file.delete();
- thumbnailFile.delete();
- } catch (Exception ex) {
- log.error(ex.getMessage(), ex);
- }
- }
- }