网络地址转File对象
作者:互联网
public static File inputStreamToFile(String url, String name) throws Exception{
File file=null;
try {
HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
httpUrl.connect();
InputStream ins=httpUrl.getInputStream();
file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
httpUrl.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
public static void main(String[] args) throws Exception {
File file = inputStreamToFile("https://ibsbjstar.ccb.com.cn/NCCB_Encoder/Encoder?CODE=EUSo4Twu6YRk2JDPJ9OnpfgrdDObpGwKBGOTqVwnBfWAvjCP5UOFuig6hySmrww2hXVIqDgb5NUOrvwI5NRuqtQ3pXcRVh", "aa.png");
}
标签:File,对象,httpUrl,ins,网络地址,file,new,String 来源: https://blog.csdn.net/Ting1king/article/details/121931248