JAVA下載圖片

測試用例如下, 所有數據是字節傳輸,可以下載任務文件(有權限)

    @Test
    public void test() throws Exception{
        // 獲取媒體文件的輸入流(讀取文件)
        String mediaFileUrl = "http://img10.360buyimg.com/imgzone/jfs/t1/64129/40/1652/208494/5d0062fdE5b875f8f/1336f14c55be3ef8.jpg";
        URL mediaUrl = new URL(mediaFileUrl);
        HttpURLConnection mediaConn = (HttpURLConnection) mediaUrl.openConnection();
        mediaConn.setDoOutput(true);
        mediaConn.setRequestMethod("GET");
        // 從請求頭中獲取內容類型
        String contentType = mediaConn.getHeaderField("Content-Type");
        // 根據內容類型判斷文件擴展名
        String fileExt = CommonUtils.getFileExt(contentType);
        BufferedInputStream bis = new BufferedInputStream(mediaConn.getInputStream());
        String file = "D:/test/download/" + UUID.randomUUID().toString() + "." + fileExt;
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        byte[] buf = new byte[8192];
        int size = 0;
        while ((size = bis.read(buf)) != -1) {
            // 將媒體文件寫到輸出流
            outputStream.write(buf, 0, size);
        }
        outputStream.close();
        bis.close();
        mediaConn.disconnect();
        System.out.println("Fin");
    }

 文件byte實際大小,並一次性輸出流

    @Test
    public void test3() throws Exception{
        // 獲取媒體文件的輸入流(讀取文件)
        String mediaFileUrl = "http://img10.360buyimg.com/imgzone/jfs/t1/40966/18/6468/250346/5d0062fdE2ad7d274/8f71146ab69ffa56.jpg";
        URL mediaUrl = new URL(mediaFileUrl);
        HttpURLConnection mediaConn = (HttpURLConnection) mediaUrl.openConnection();
        mediaConn.setDoOutput(true);
        mediaConn.setRequestMethod("GET");
//        int length = mediaConn.getContentLength();
//        System.out.println("getContentLengthLong:" + mediaConn.getContentLengthLong());
//        System.out.println("getContentLength:" + length);
        // 從請求頭中獲取內容類型
        String contentType = mediaConn.getHeaderField("Content-Type");
        // 根據內容類型判斷文件擴展名
        String fileExt = CommonUtils.getFileExt(contentType);
        BufferedInputStream bis = new BufferedInputStream(mediaConn.getInputStream());
        String file = "D:/test/download-64/" + UUID.randomUUID().toString() + "." + fileExt;
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        byte[] buf = new byte[8192];
        int size = 0;
        ByteBuffer byteBuffer = ByteBuffer.allocate(250346*3);
        while ((size = bis.read(buf)) != -1) {
            // 將媒體文件寫到輸出流
            outputStream.write(buf, 0, size);
            byteBuffer.put(buf, 0, size);
        }
        System.out.println("byteBuffer position2 limit:" + byteBuffer.limit());
        byteBuffer.flip();
        byte[] target = new byte[byteBuffer.limit()];
        byteBuffer.get(target);
        System.out.println("byteBuffer position end:" + byteBuffer.position());
        System.out.println("target length end:" + target.length);

        String file2 = "D:/test/download-64/" + UUID.randomUUID().toString() + "PPP." + fileExt;
        OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(file2));
        outputStream2.write(target);
        outputStream2.close();

        outputStream.close();
        bis.close();
        mediaConn.disconnect();
        System.out.println("Fin");
    }

 

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