ImageUpload.java
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:6k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

JavaScript

  1. package com.chinacannel.common;
  2. import java.io.*;
  3. import org.apache.struts.upload.*;
  4. import com.chinacannel.entity.Image;
  5. import java.awt.image.BufferedImage;
  6. import javax.imageio.ImageIO;
  7. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  8. import com.sun.image.codec.jpeg.JPEGCodec;
  9. import java.util.Date;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.apache.commons.logging.Log;
  12. import javax.servlet.ServletRequest;
  13. public class ImageUpload {
  14.     private static Log log = LogFactory.getLog(ImageUpload.class);
  15.     public ImageUpload() {
  16.     }
  17.     public Image saveImage(FormFile file, ServletRequest servletRequest) {
  18.         return this.saveImage(file, servletRequest, true);
  19.     }
  20.     public Image saveImage(FormFile file,ServletRequest servletRequest,boolean createThumbnail) {
  21.         if(file==null){
  22.             return null;
  23.         }
  24.         InputStream stream = null;
  25.         OutputStream bos = null;
  26.         try {
  27.             stream = file.getInputStream(); //把文件读入
  28.             String path = servletRequest.getRealPath("/"); //取当前系统路径
  29.             //         String filePath = path + file.getFileName();
  30.             path += Params.getImgPath();
  31.             String fileName = file.getFileName();
  32.             Date d = new Date();
  33.             String nFileName = String.valueOf(d.getTime()) + fileName;
  34.             bos = new FileOutputStream(path + nFileName, true); //建立一个上传文件的输出流
  35.             log.info("-------saveImage--------->"+path+nFileName);
  36.             int bytesRead = 0;
  37.             byte[] buffer = new byte[8192];
  38.             while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
  39.                 bos.write(buffer, 0, bytesRead); //将文件写入服务器
  40.             }
  41.             String breFileName = "";
  42.             if(createThumbnail){
  43.                 breFileName = this.CreateThumbnail(nFileName, servletRequest);
  44.             }
  45.            return this.saveToDB(fileName, nFileName, Params.getImgPath(), breFileName,
  46.                           Params.getThumbnailPath());
  47.         } catch (FileNotFoundException e) {
  48.             log.error(e.getMessage(), e);
  49.             return null;
  50.         } catch (Exception ex) {
  51.             log.error(ex.getMessage(), ex);
  52.             return null;
  53.         } finally {
  54.             try {
  55.                 bos.close();
  56.                 stream.close();
  57.             } catch (Exception ex1) {
  58.                 log.error(ex1.getMessage(), ex1);
  59.             }
  60.         }
  61.     }
  62.     protected String CreateThumbnail(String fileName,ServletRequest servletRequest) throws
  63.             Exception {
  64.         String path = servletRequest.getRealPath("/")+Params.getImgPath();
  65.         log.info("------CreateThumbnail---------->"+path);
  66.         File f = new File(path + fileName);
  67.         BufferedImage bi = null;
  68.         try {
  69.             bi = ImageIO.read(f);
  70.         } catch (IOException ex2) {
  71.             throw new IOException(ex2.getMessage());
  72.         }
  73.         String fn = "";
  74.         FileOutputStream out = null;
  75.         String[] s = this.splitFileName(fileName);
  76.         String name = s[0];
  77.         String ext = s[1];
  78.         if ("jpg".equals(ext) || "jpeg".equals(ext)) {
  79.             fn = name + "Bre." + ext;
  80.         } else {
  81.             fn = name + "Bre.png";
  82.         }
  83.         int nw = bi.getWidth() / 2;
  84.         int nh = bi.getHeight() / 2;
  85.         BufferedImage tag = new BufferedImage(nw, nh,
  86.                                               BufferedImage.TYPE_INT_RGB);
  87.         tag.getGraphics().drawImage(bi, 0, 0, nw, nh, null); //绘制缩小后的图
  88.         try {
  89.             out = new FileOutputStream(servletRequest.getRealPath("/")+Params.getThumbnailPath() + fn); //输出到文件流
  90.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  91.             encoder.encode(tag); //近JPEG编码
  92.             out.close();
  93.         } catch(Exception ex){
  94.             log.error(ex.getMessage(), ex);
  95.         }
  96.         finally {
  97.             out.close();
  98.         }
  99.         return fn;
  100.     }
  101.     protected String[] splitFileName(String fileName) {
  102.         int a = fileName.lastIndexOf(".");
  103.         String[] r = new String[2];
  104.         r[0] = fileName.substring(0, a);
  105.         r[1] = fileName.substring(a + 1);
  106.         return r;
  107.     }
  108.     protected Image saveToDB(String fileName, String reallyName, String path,
  109.                         String ThumbnailName, String ThumbnailPath) {
  110.         CommonDAO dao = new CommonDAO();
  111.         Image img = new Image();
  112.         img.setFilename(fileName);
  113.         img.setReallyname(reallyName);
  114.         img.setPath(path);
  115.         img.setThumbnailname(ThumbnailName);
  116.         img.setThumbnailpath(ThumbnailPath);
  117.         try {
  118.             dao.createObject(img);
  119.             return img;
  120.         } catch (DAOException ex) {
  121.             log.error(ex.getMessage(), ex);
  122.             return null;
  123.         }
  124.     }
  125.     public void deleteImage(int imgId){
  126.         CommonDAO dao = new CommonDAO();
  127.         Image image=null;
  128.         try {
  129.             image=(Image)dao.loadObject(new Long(imgId), Image.class);
  130.             dao.removeObject(image);
  131.             this.deleteFile(image);
  132.         } catch (DAOException ex) {
  133.             log.error(ex.getMessage(), ex);
  134.         }
  135.     }
  136.     protected void deleteFile(Image img){
  137.         if(img==null){
  138.             return;
  139.         }
  140.         try {
  141.             String imgName = img.getPath() + img.getFilename();
  142.             String imgThumbnailName = img.getThumbnailpath() +
  143.                                       img.getThumbnailname();
  144.             File file = new File(imgName);
  145.             File thumbnailFile = new File(imgThumbnailName);
  146.             file.delete();
  147.             thumbnailFile.delete();
  148.         } catch (Exception ex) {
  149.             log.error(ex.getMessage(), ex);
  150.         }
  151.     }
  152. }