java读取网络文件和本地文件

java读取网络文件和本地文件:

package com.mybatisplustest.test;

import java.io.*;
import java.net.URL;

/**
 * Created by Administrator on 2023/12/21.
 */
public class Test {
    public static void main(String[] args) {
        // 读取网络文件
         readNetFile();
        // 读取本地文件
//        readLocalFile();
    }

    /**
     * 读取本地文件
     * @return void
     */
    private static void readLocalFile() {
        // 可以相对路径或绝对路径
        // 绝对路径
        //String sourcePath = "D:/test/t1/t.jpg";
        // 相对路径,如果首位不是/,表示相对于(父级目录)当前项目路径:绝对路径是D:\IdeaProjects\springboot-mybatisplustest\test\t1\t.jpg
        // 则D:\IdeaProjects\springboot-mybatisplustest\是当前项目路径
        // String sourcePath = "test/t1/t.jpg";
        // 如果首位是/,则是相对于(根目录)根目录。绝对路径是D:\test\t1\t.jpg,则根目录是D:。
        String sourcePath = "/test/t1/t.jpg";
        File sourceFile = new File(sourcePath);
        // 打印绝对路径
        System.out.println(sourceFile.getAbsolutePath());
        if(!sourceFile.exists()){
            System.out.println("文件不存在");
            return;
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(sourceFile);
            String path = "D:/test/t2";
            // 如果目标文件存在上级目录,需要判断目录是否存在。否则会异常。
            // 文件目录。
            File filePath = new File(path);
            File file = new File(path + "/t.jpg");
            // 需要判断文件目录是否存在,否则会异常。
            if(!filePath.exists()){
                /*
                mkdir():父目录要存在,否则创建不成功。创建单个目录。
                mkdirs():父目录不存在也能创建成功。创建多级目录。
                 */
                filePath.mkdirs();
            }
            os = new FileOutputStream(file);
            // 缓冲1024byte=1kb。每次读取1kb并写入。
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1){
                // byte b[], int off, int len
                // 缓冲数据,开始位置,字节数
                os.write(buffer,0,len);
            }
            System.out.println("完成");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 读取网络文件
     * @return void
     */
    private static void readNetFile() {
        InputStream is = null;
        OutputStream os = null;
        try {
            // 读取网络图片
            URL urlFile = new URL("https://img2022.cnblogs.com/blog/1003101/202208/1003101-20220802095124634-1241340631.png");
            is = urlFile.openStream();
            String path = "D:/test/t1";
            // 如果目标文件存在上级目录,需要判断目录是否存在。否则会异常。
            // 文件目录。
            File filePath = new File(path);
            File file = new File(path + "/t.jpg");
            // 需要判断文件目录是否存在,否则会异常。
            if(!filePath.exists()){
                /*
                mkdir():父目录要存在,否则创建不成功。创建单个目录。
                mkdirs():父目录不存在也能创建成功。创建多级目录。
                 */
                filePath.mkdirs();
            }
            os = new FileOutputStream(file);
            // 缓冲1024byte=1kb。每次读取1kb并写入。
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1){
                // byte b[], int off, int len
                // 缓冲数据,开始位置,字节数
                os.write(buffer,0,len);
            }
            System.out.println("完成");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章