导入包:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 测试
*/
public static void main(String[] args) {
try {
String destUrl = "https://ibsbjstar.ccb.com.cn/NCCB_Encoder/Encoder?CODE=EUSo4Twu6YRk2JDPJ9OnpfgrdDObpGwKBGOTqVwnBfWAvjCP5UOFuig6hySmrww2hXVIqDgb5NUOrvwI5NRuqtQ3pXcRVh";
HttpURLConnection httpUrl = (HttpURLConnection) new URL(destUrl).openConnection();
httpUrl.connect();
File file = FileUtil.inputStreamToFile(httpUrl.getInputStream(),"url.png");
System.out.println("111====>>>>"+file.getPath());
httpUrl.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 工具类
* inputStream 转 File
*/
public static File inputStreamToFile(InputStream ins, String name) throws Exception{
File 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();
return file;
}
打印输出:

2万+

被折叠的 条评论
为什么被折叠?



