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();
                }
            }
        }
    }
}

 

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