weixin_42298398 2009-07-21 10:17
浏览 194
已采纳

commons-fileupload上传图片获取图片长宽?

用commons-fileupload上传图片时怎样获取图片的长宽,如300*200这种类型.怎样做?
若用js怎么实现获取图片的长宽?
请哪位仁兄快快帮我解答一下.

  • 写回答

2条回答 默认 最新

  • wanghaolovezlq 2009-07-21 10:19
    关注

    [code="java"]
    /*

    • Created on 2005-1-14 */ package org.common.util;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import javax.imageio.ImageIO;

    import org.apache.log4j.Logger;
    import org.apache.struts.upload.FormFile;

    /**

    • @author 张欣
      */
      public class UploadFile {
      //获得日志服务
      static Logger logger = Logger.getLogger( UploadFile.class.getName (); ); ;

      public static String upLoad(FormFile file,String uploadDir,String newName,int bigersize); throws FileNotFoundException, IOException{

      String data = null;
      String location = null;
      String totalname;
      String extend;
      StringBuffer fileName;
      String contentType;
      
      int size;        
      int i;
      int width;
      int height;
      File dirPath;
      InputStream inputStream = null;
      OutputStream outputStream;
      
      //得到上传文件的全名,包括扩展名
      totalname = file.getFileName();;             
       //得到扩展名
      i = totalname.lastIndexOf(".");;
      
       //如果没有上传图片,则返回空
       if(i==-1);{
          return location;
       }
      
       extend = totalname.substring(i);;
       //得到文件大小
       size = file.getFileSize();;
      
       logger.info("#上传图片扩展名为:# =" + extend);;
       logger.info("#上传图片大小为:# =" + size/1024 + "K");;
      
       if(!extend.equalsIgnoreCase(".jpg");&&!extend.equalsIgnoreCase(".gif");&&!extend.equalsIgnoreCase(".jpeg");&&!extend.equalsIgnoreCase(".bmp");&&
              !extend.equalsIgnoreCase(".png");&&!extend.equalsIgnoreCase(".doc");&&!extend.equalsIgnoreCase(".txt");&&!extend.equalsIgnoreCase(".rar");&&!extend.equalsIgnoreCase(".zip");&&!extend.equalsIgnoreCase(".pdf"););{
          return "filetypeWrong";
       }
      
       if(size>1024*bigersize);{
          return "filesizeWrong";
       }
      
       //重命名该文件
       fileName = new StringBuffer(newName + extend);;
       fileName.toString();;     
      
       //判断宽高
      inputStream = file.getInputStream();;  
      
      BufferedImage bi = ImageIO.read(inputStream);; 
      // another interface such as:read(new File(filename););; 
      System.out.println("Width=" + bi.getWidth(););; 
      System.out.println("Height=" + bi.getHeight(););; 
      width = bi.getWidth();;
      height = bi.getHeight();;
      if(width>1500 || height>1500);{
          return "filetoohigh";
      }
      
      //在图片上写入网站标示的文字
      //利用Graphics2D的新特性.
      Graphics2D g2D = bi.createGraphics();; 
      //定义字体(楷体或宋体等); 及其大小
      Font myfont=new Font("\u5b8b\u4f53",Font.PLAIN,20);;
      g2D.setFont(myfont);;
      //字体颜色为黑
      g2D.setColor(Color.BLACK);;
      
      //需要写在图片上文字 注意:中文需要操作系统支持中文 如中文window,
      //linux下要安装中文字库,并使jdk1.4支持这个中文
      String strs="hello everyone 中 文";
      
      //在离顶部 和左边各为20处 写入文字,该处使用了对中文处理的getBytes功能.
      g2D.drawString(new String(strs.getBytes("ISO8859_1"););,20,20);;
      
      //必须使用,表示完成 
      g2D.dispose();; 
      
      try {
      //使用ImageIO写入图片 
      ImageIO.write(bi, ext, ThF);;
      
      //原来是使用com.sun.image.codec.jpeg.*的功能,如下.需要下载com.sun.image.codec.jpeg
      //FileOutputStream out = new FileOutputStream(ThF);;
      //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);;
      //encoder.encode(Bi);;
      //out.close();;
      
      }catch (Exception ex); {
      throw new Exception(" ImageIo.write error in CreatThum.: "+ex.getMessage(););; 
      }
      
      //上传
      dirPath = new File(uploadDir);;
      if (!dirPath.exists();); {
          dirPath.mkdirs();;
      }        
      outputStream = new FileOutputStream(uploadDir + fileName);;
      int bytesRead = 0;
      byte[] buffer = new byte[8192];
      while ((bytesRead = inputStream.read(buffer, 0, 8192);); != -1); {
          outputStream.write(buffer, 0, bytesRead);;
      }
      
      location = dirPath.getAbsolutePath();
          + System.getProperty("file.separator"); + fileName;
      
      outputStream.close();;
      inputStream.close();;
      file.destroy();;
      
      return location;
      

      }
      }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿