Java鏈接url下載圖片

                //下載
                url = urlPreGlobalDoc + d.getEmployeeId() + "/" + d.getLocalPath();
                path = pathPreGlobalDoc + d.getFilePath();
                this.downloadPicture(url, path);

 

// 鏈接url下載圖片
    private void downloadPicture(String urlList, String path) {
        try {
            URL url = null;
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());
            FileOutputStream fileOutputStream = new FileOutputStream(
                    new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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