注意这种方式会在计算机根目录生成文件, public static File httpsGetImagAsFile(String getUrl) throws Exception{ HttpsURLConnection httpURLConnection = null; try { // 1. 得到访问地址的URL URL url = new URL(getUrl); // 2. 得到网络访问对象java.net.HttpURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection(); /* 3. 设置请求参数(过期时间,输入、输出流、访问方式),以流的形式进行连接 */ // 设置是否向HttpURLConnection输出 httpURLConnection.setDoOutput(false); // 设置是否从httpUrlConnection读入 httpURLConnection.setDoInput(true); // 设置请求方式 默认为GET httpURLConnection.setRequestMethod("GET"); // 设置是否使用缓存 httpURLConnection.setUseCaches(false); // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向 httpURLConnection.setInstanceFollowRedirects(true); // 设置连接超时时间 httpURLConnection.setConnectTimeout(3000); // 设置读取返回超时时间 httpURLConnection.setReadTimeout(3000);
java HttpsURLConnection get方式请求网络图片保存为File
最新推荐文章于 2024-06-09 16:06:06 发布