增加重试,修改JDK版本
This commit is contained in:
@@ -1,25 +1,22 @@
|
||||
package com.zdjizhi.syncfile.utils;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.zdjizhi.syncfile.entity.PostFileResponse;
|
||||
import com.zdjizhi.syncfile.monitor.MonitorProperties;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@Component
|
||||
public class HttpUtil {
|
||||
private static Log log = LogFactory.get();
|
||||
@@ -31,19 +28,19 @@ public class HttpUtil {
|
||||
@Autowired
|
||||
MonitorProperties monitorProperties;
|
||||
|
||||
public InputStream httpGetInputStream(String url) {
|
||||
InputStream result = null;
|
||||
public byte[] httpGetFile(String url) {
|
||||
byte[] data = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
httpGet.setConfig(requestConfig);
|
||||
response = httpClient.execute(httpGet);
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
result = IOUtils.toBufferedInputStream(response.getEntity().getContent());
|
||||
data = IoUtil.readBytes(response.getEntity().getContent());
|
||||
log.info("get file success. current url: {}", url);
|
||||
monitorProperties.addDownloadFileSuccessCount();
|
||||
monitorProperties.addDownloadFileSize(Integer.parseInt(response.getFirstHeader("Content-Length").getValue()));
|
||||
}else if (response.getStatusLine().getStatusCode() == 500){
|
||||
monitorProperties.addDownloadFileSize(data.length);
|
||||
} else if (response.getStatusLine().getStatusCode() == 500) {
|
||||
log.error("get file error. Hos service error, please check hos. current url: {}", url);
|
||||
monitorProperties.addHosError();
|
||||
} else {
|
||||
@@ -56,40 +53,40 @@ public class HttpUtil {
|
||||
} finally {
|
||||
IoUtil.close(response);
|
||||
}
|
||||
return result;
|
||||
return data;
|
||||
}
|
||||
|
||||
public boolean httpPostInputStream(String url, InputStream data) {
|
||||
public boolean httpPostFile(String url, byte[] file) {
|
||||
boolean isSuccess = false;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.setConfig(requestConfig);
|
||||
httpPost.setEntity(new InputStreamEntity(data));
|
||||
httpPost.setEntity(new ByteArrayEntity(file));
|
||||
response = httpClient.execute(httpPost);
|
||||
String responseEntity = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||
log.info("post file response: " + responseEntity);
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
String responseEntity = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||
JSONObject jsonObj = (JSONObject) JSON.parse(responseEntity);
|
||||
if(jsonObj!=null){
|
||||
if (responseEntity.contains("\"code\":200")) {
|
||||
PostFileResponse postFileResponse = JSON.toJavaObject(jsonObj, PostFileResponse.class);
|
||||
if (responseEntity.contains("\"code\":200")) {
|
||||
if (JSONUtil.isJsonObj(responseEntity)) {
|
||||
JSONObject jsonObj = JSONUtil.parseObj(responseEntity);
|
||||
PostFileResponse postFileResponse = JSONUtil.toBean(jsonObj, PostFileResponse.class);
|
||||
isSuccess = true;
|
||||
log.info("post file success. current url: {}, msg: {}", url, responseEntity);
|
||||
monitorProperties.addPostFileSuccessCount();
|
||||
monitorProperties.addPostFileSize(postFileResponse.getData().getFileSize());
|
||||
} else {
|
||||
log.error("post file error. current url: {}, msg: {}", url,responseEntity);
|
||||
monitorProperties.addFileSyncError();
|
||||
log.error("post file successfully response is not json data. current url: {}, msg: {}", url, responseEntity);
|
||||
}
|
||||
}else {
|
||||
log.error("post file error, response body error. current url: {}", url);
|
||||
monitorProperties.addOssError();
|
||||
} else {
|
||||
log.error("post file error. current url: {}, msg: {}", url, responseEntity);
|
||||
monitorProperties.addFileSyncError();
|
||||
}
|
||||
} else if(response.getStatusLine().getStatusCode() == 500){
|
||||
log.error("post file error. Oss service error.current url: {}, code: 500, msg: {}", url, response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8"));
|
||||
} else if (response.getStatusLine().getStatusCode() == 500) {
|
||||
log.error("post file error. Oss service error.current url: {}, code: 500, msg: {}", url, response.getStatusLine().getStatusCode(), responseEntity);
|
||||
monitorProperties.addOssError();
|
||||
}else {
|
||||
log.error("post file error. current url: {}, code: {}, msg: {}", url, response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8"));
|
||||
} else {
|
||||
log.error("post file error. current url: {}, code: {}, msg: {}", url, response.getStatusLine().getStatusCode(), responseEntity);
|
||||
monitorProperties.addFileSyncError();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user