zoukankan      html  css  js  c++  java
  • java io流根据url读取图片

    //获取图片大小
    public void readFileSize(String url,HttpServletRequest request){

    //根路径

    File file = new File(request.getRealPath("/")+url);
    InputStream is = null;
    FileImageOutputStream imageOutput=null;
    //按图片名截取
    String fileName=file.getName();
    //按后缀截取
    String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
    try {
    is = new BufferedInputStream(new FileInputStream(file));
    int i=is.available()/1024;
    if (i>30.4) {
    // app.base.data_excel  存图片的路径
    File imagePath = new File(Config.getKey("app.base.data_excel")+"picdatafor1/");
    if (!imagePath.exists()) {
    imagePath.mkdir();
    }
    byte[] getData = readInputStream(request.getRealPath("/")+url);
    imageOutput = new FileImageOutputStream(new File(Config.getKey("app.base.data_excel")+"picdatafor1/"+fileName));
    imageOutput.write(getData, 0, getData.length);
    imageOutput.flush();
    imageOutput.close();
    }else if(i<=30.4 && "png".equals(prefix)){

    FileWriter fw = new FileWriter(new File(Config.getKey("app.base.data_excel")+"pic.txt"),true);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(fileName);
    bw.newLine();
    bw.flush();
    bw.close();
    fw.close();

    // 输出图片路径
    byte[] getData = readInputStream(request.getRealPath("/")+url);
    File imagePath = new File(Config.getKey("app.base.data_excel")+"picdatafor0/");
    if (!imagePath.exists()) {
    imagePath.mkdir();
    }

    imageOutput = new FileImageOutputStream(new File(Config.getKey("app.base.data_excel")+"picdatafor0/"+fileName),t);
    imageOutput.write(getData, 0, getData.length);
    imageOutput.flush();
    imageOutput.close();

    }else {
    byte[] getData = readInputStream(request.getRealPath("/")+url);
    File imagePath = new File(Config.getKey("app.base.data_excel")+"picdatafor2/");
    if (!imagePath.exists()) {
    imagePath.mkdir();
    }

    imageOutput = new FileImageOutputStream(new File(Config.getKey("app.base.data_excel")+"picdatafor2/"+fileName));
    imageOutput.write(getData, 0, getData.length);
    imageOutput.flush();
    imageOutput.close();
    }
    } catch (FileNotFoundException e) {
    // System.out.println("url不存在");
    } catch (IOException e) {
    e.printStackTrace();
    }

    finally{
    if (is != null) {
    try {
    is.close(); // 关闭流
    } catch (IOException e) {
    System.out.println(e.getMessage());
    }
    }
    }

    }

    // 根据url读取文件
    public static byte[] readInputStream(String url) throws IOException {
    byte[] data = null;
    FileImageInputStream input = new FileImageInputStream(new File(url));
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buf = new byte[1024*1024];
    int numBytesRead = 0;
    while ((numBytesRead = input.read(buf)) != -1) {
    output.write(buf, 0, numBytesRead);
    }
    data = output.toByteArray();
    output.close();
    input.close();
    return data;
    }

  • 相关阅读:
    动态类型
    unlink与close关系
    Google 历年笔试面试30题
    UNIX网络编程 卷2 源代码使用
    centos安装telnet
    python中安装第三方模块
    Linux平台 Oracle 18c RAC安装Part1:准备工作
    RHEL7 配置iSCSI模拟环境
    Solaris 10主机名和IP地址步骤
    设置sqlplus不显示除查询结果外的信息
  • 原文地址:https://www.cnblogs.com/0c7x4/p/8079380.html
Copyright © 2011-2022 走看看