JAVA中URL網絡傳輸的文件的下載工具類

public class StreamUtils {

    /**
     * @Description: 將字節的輸入流轉換成字節數組進行網絡傳輸
     * @Param: InputStream
     * @return:
     * @Author: LingFeng
     * @Date: 2019/11/21
     */
    public static byte[] InputStreamToByte(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        try {
            while ((len = inputStream.read(b)) != -1) {
                byteArrayOutputStream.write(b, 0, len);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return byteArrayOutputStream.toByteArray();

    }

    /**
     * @Description: 用於對網絡傳輸的文件的下載
     * @Param: URL:網絡傳輸;HttpServletResponse:響應;fileName:文件的名字
     * @return:
     * @Author: LingFeng
     * @Date: 2019/11/21
     *  URL用於網絡,所以帶有明顯的protocol,而且對於中文及符號支持的很不好。
     * File就是我們平常系統中的文件路徑了,對於中文及符號都支持,但是已經沒有protocol了。
     * 所以,雖然兩者都可以表示文件路徑,但是卻不能混用了。
     *   BufferedInputStream繼承於FilterInputStream,提供緩衝輸入流功能。緩衝輸入流相對於普通輸入流的優勢是,
     * 它提供了一個緩衝數組,每次調用read方法的時候,它首先嚐試從緩衝區裏讀取數據,若讀取失敗(緩衝區無可讀數據),
     * 則選擇從物理數據源(譬如文件)讀取新數據(這裏會嘗試儘可能讀取多的字節)放入到緩衝區中,
     * 最後再將緩衝區中的內容部分或全部返回給用戶.
     * 由於從緩衝區裏讀取數據遠比直接從物理數據源(譬如文件)讀取速度快。
     */

    public static void uploadFileByUrl(URL url, HttpServletResponse response, String fileName) {
        InputStream inputStream = null;//文件的字節輸入流
        OutputStream outputStream = null;//文件的字節輸出流
        BufferedInputStream bufferedInputStream = null;//輸入緩衝流
        BufferedOutputStream bufferedOutputStream = null;//輸出緩衝流
        try {
            inputStream = url.openStream();
            bufferedInputStream = new BufferedInputStream(inputStream);
            response.reset();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
            outputStream = response.getOutputStream();
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = bufferedInputStream.read(b)) != -1) {
                bufferedOutputStream.write(b, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outputStream != null) {
                try {
                    bufferedOutputStream.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }




}

URL urlConet = new URL("http://192.168.25.133/group1/M00/00/00/wKgZhV2QVYmATWYsAACZeklp3R4679.jpg");

 String fileName=new String("測試.pnj");

 

發佈了44 篇原創文章 · 獲贊 12 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章