zoukankan      html  css  js  c++  java
  • Java 实现网络图片的读取与下载

     1     //网络图片的下载,读取与删除
     2     public static void fileDowAndDel(String httpurl){
     3         try {
     4             URL url = new URL(httpurl);
     5             URLConnection uc = url.openConnection();
     6             //String path = "D:"+File.separator+"tempPicture.jpg";//文件保存路径
     7             String path = "/site/wimanager"+File.separator+"tempPicture.jpg";//文件保存路径
     8             FileOutputStream os = new FileOutputStream(path);
     9             
    10             InputStream is = uc.getInputStream();
    11             byte[] b = new byte[1024];
    12             int len = 0;
    13             while((len=is.read(b))!=-1){
    14                 os.write(b,0,len);
    15             }
    16             os.close();
    17             is.close();
    18             System.out.println("文件大小:"+(uc.getContentLength()/1024)+"KB");
    19             System.out.println("下载成功,文件保存在:"+path);
    20             
    21             //读取本地图片信息
    22             File file = new File(path);
    23             BufferedImage bi = null;
    24             try {
    25                 bi = ImageIO.read(file);
    26             } catch (IOException e) {
    27                 e.printStackTrace();
    28             }
    29             int width = bi.getWidth();
    30             int height = bi.getHeight();
    31             System.out.println("图片尺寸,宽:"+width+",高:"+height);
    32             
    33             //删除临时文件,路径为文件且不为空则进行删除  
    34             if (file.isFile() && file.exists()) {  
    35                 file.delete();
    36                 //System.out.println("删除成功,文件路径:"+path);
    37             }
    38         } catch (Exception e) {
    39             e.printStackTrace();
    40         }
    41     }

     文件和文件夹的创建

     1 //创建文件夹
     2 public class Test{
     3     public static void main(String[] args) {
     4         //1、判断文件是否存在,不存在创建文件
     5         //File file=new File("C:\Users\QPING\Desktop\JavaScript\2.htm");//windows下
     6         File file=new File("/site/images/qiyinwang/userid/xxxxx.jpg");//linux下
     7         if(!file.exists()){    
     8             try {    
     9                 file.createNewFile();    
    10             } catch (IOException e) {    
    11                 e.printStackTrace();    
    12             }    
    13         }    
    14 
    15         //2、判断文件夹是否存在,不存在创建文件夹
    16         //File file1 =new File("C:\Users\QPING\Desktop\JavaScript");//windows下
    17         File file1 = new File("/site/images/qiyinwang/userid");//linux下
    18         //如果文件夹不存在则创建    
    19         if  (!file1 .exists()  && !file1 .isDirectory()){       
    20             System.out.println("目录不存在,创建");  
    21             file1 .mkdirs();    
    22         } else {  
    23             System.out.println("目录存在");
    24         }  
    25     }
    26 }
  • 相关阅读:
    P1001 A+B Problem
    NOIP2015D1T2 信息传递
    海淀区赛游记。。。。
    P3375 【模板】KMP字符串匹配
    Print Article HDU
    BZOJ-2-4870: [Shoi2017]组合数问题 矩阵优化 DP
    BZOJ-1- 4868: [Shoi2017]期末考试-三分
    #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队
    湖南大学第十四届ACM程序设计新生杯(重现赛)
    Codeforces Round #530 (Div. 2)
  • 原文地址:https://www.cnblogs.com/wbxk/p/6202086.html
Copyright © 2011-2022 走看看