通過網絡URL獲取文件


URL imageUrl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) imageUrl.openConnection();
inputStream = urlConnection.getInputStream();

 

File file = new File("person_"+faceBookShareBean.getPicture());
FileUtils.copyURLToFile(new URL(faceBookShareBean.getPicture()), file);
resultId = facebookClient.postVedio(faceBookShareBean.getAccessToken(), file, faceBookShareBean.getDescription());
file.delete();

 

 

File file = downUrlTxt("11.jpg", "https://mailfile.onloon.co/361963894425540060/Hydrangeas.jpg", "D:\\wuwanhua\\pic");

 

public static File downUrlTxt(String fileName,String fileUrl,String downPath){
        File savePath = new File(downPath);
        if (!savePath.exists()) {
            savePath.mkdir();
        }
        String[] urlname = fileUrl.split("/");
        int len = urlname.length-1;
        String uname = urlname[len];//獲取文件名
        try {
            File file = new File(savePath+"/"+uname);//創建新文件
            if(file!=null && !file.exists()){
                file.createNewFile();
            }
            OutputStream oputstream = new FileOutputStream(file);
            URL url = new URL(fileUrl);
            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            uc.setDoInput(true);//設置是否要從 URL 連接讀取數據,默認為true
            uc.connect();
            InputStream iputstream = uc.getInputStream();
            System.out.println("file size is:"+uc.getContentLength());//打印文件長度
            byte[] buffer = new byte[4*1024];
            int byteRead = -1;
            while((byteRead=(iputstream.read(buffer)))!= -1){
                oputstream.write(buffer, 0, byteRead);
            }
            oputstream.flush();
            iputstream.close();
            oputstream.close();
            return file;
        } catch (Exception e) {
            System.out.println("讀取失敗!");
            e.printStackTrace();
            return null;
        }
//        System.out.println("生成文件路徑:"+downPath+fileName);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM