java获取上传图片长宽

private static final String CONTENT_TYPE = "text/html;charset=UTF-8";
public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   response.setContentType(CONTENT_TYPE);
   request.setCharacterEncoding("UTF-8");
  
   String path = request.getRealPath("upload/newImage");

   File file = new File(path);
   if (!file.exists()) {
    file.mkdir();
   } else {
    DiskFileItemFactory factory = new DiskFileItemFactory();

    factory.setRepository(new File(path));

    factory.setSizeThreshold(1024 * 1024);

    ServletFileUpload sfu = new ServletFileUpload(factory);
   
    try {
     List<FileItem> _cache=sfu.parseRequest(request);
     for(FileItem item :_cache) {
      if(!item.isFormField()) {
       String name=item.getFieldName();
       String filedValue=item.getName();
       int start=filedValue.lastIndexOf("\\");
       String value=filedValue.substring(start+1);
       request.setAttribute(name, value);
       OutputStream os=new FileOutputStream(new File(path,value));
      
       InputStream iis=item.getInputStream();
       BufferedImage bi=ImageIO.read(iis);
       int width=bi.getWidth();
      
       int height=bi.getHeight();
      
       System.out.println("width="+width);
       System.out.println("height="+height);
      
       iis.close();
      
       InputStream is=item.getInputStream();
      
       byte[] buffer=new byte[500];
      
       int length=0;
      
       while((length=is.read(buffer))!=-1) {
        os.write(buffer, 0, length);
       }
       os.close();
      
      
      
       is.close();
      }
     }
    
     request.getRequestDispatcher("uploadImageSuccess.jsp").forward(request, response);
    } catch (FileUploadException e) {
     System.out.println(e.getMessage());
     e.printStackTrace();
    }

   }
}

(个人微信号)   (技术公众号)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值