通过网络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