图片url地址转换成file文件

public static File getFile(String url) throws Exception {
    //对本地文件命名
    String fileName = url.substring(url.lastIndexOf("."),url.length());
    File file = null;

    URL urlfile;
    InputStream inStream = null;
    OutputStream os = null;
    try {
        file = File.createTempFile("net_url", fileName);
        //下载
        urlfile = new URL(url);
        inStream = urlfile.openStream();
        os = new FileOutputStream(file);

        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != os) {
                os.close();
            }
            if (null != inStream) {
                inStream.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return file;
}
    原文作者:海阔天空6688
    原文地址: https://blog.csdn.net/xiaopihair123/article/details/106992716
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞