|
@@ -0,0 +1,82 @@
|
|
|
+package com.ozs.service.utils;
|
|
|
+
|
|
|
+import com.ozs.common.utils.MinioUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wyy
|
|
|
+ * @subject
|
|
|
+ * @creat 2023/4/20
|
|
|
+ */
|
|
|
+public class HttpGetUtil {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(HttpGetUtil.class);
|
|
|
+ @Resource
|
|
|
+ public static MinioUtils minioUtils;
|
|
|
+
|
|
|
+ public static void getPicture(URL url,String fileName) throws IOException {
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ BufferedOutputStream bos = null;
|
|
|
+ try {
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
+ // http正文内,因此需要设为true
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ // Post 请求不能使用缓存
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ //设置本次连接是否自动重定向
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ connection.setRequestProperty("Accept-Language", "zh,zh-CN;q=0.9");
|
|
|
+ connection.setRequestProperty("connection", "keep-alive");
|
|
|
+ //设置参数类型是json格式
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
|
|
|
+ connection.setRequestProperty("Accept","application/json");
|
|
|
+ //connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
|
|
+ connection.setRequestProperty("Charsert", "UTF-8");
|
|
|
+ //设置读取时间为30秒
|
|
|
+ connection.setConnectTimeout(30 * 1000);
|
|
|
+ connection.setReadTimeout(30 * 1000);
|
|
|
+ connection.connect();
|
|
|
+
|
|
|
+ // 返回流
|
|
|
+ System.out.println("responseCode=" + connection.getResponseCode());
|
|
|
+ log.info("responseCode="+connection.getResponseCode());
|
|
|
+ if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
|
|
+ InputStream input = connection.getInputStream();
|
|
|
+ bis = new BufferedInputStream(input);
|
|
|
+ minioUtils.minIoClientUpload(input, fileName);
|
|
|
+// FileOutputStream output = new FileOutputStream(fileName);
|
|
|
+// bos = new BufferedOutputStream(output);
|
|
|
+// byte[] buffer = new byte[1024];
|
|
|
+// int size = 0;
|
|
|
+// while ((size = bis.read(buffer, 0, 1024)) != -1) {
|
|
|
+// bos.write(buffer, 0, size);
|
|
|
+// }
|
|
|
+// bos.flush();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ //先关闭外层的缓冲流,再关闭内层的流,但是在关闭外层流的同时,
|
|
|
+ //内层流也会自动的进行关闭,关于内层流的关闭,可以省略
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (bis != null) {
|
|
|
+ bis.close();
|
|
|
+ }
|
|
|
+// if (bos != null) {
|
|
|
+// bos.close();
|
|
|
+// }
|
|
|
+ if (connection != null) {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
+// os.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|