|
@ -6,6 +6,7 @@ import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.apache.commons.codec.binary.Base64; |
|
|
import org.apache.commons.codec.binary.Base64; |
|
|
|
|
|
import org.apache.commons.io.IOUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.http.HttpEntity; |
|
|
import org.apache.http.HttpEntity; |
|
|
import org.apache.http.HttpStatus; |
|
|
import org.apache.http.HttpStatus; |
|
@ -326,6 +327,23 @@ public class HttpClientManager { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Result<byte[]> getMediaByteArray(String url, String json) { |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
HttpPost httppost = new HttpPost(url); |
|
|
|
|
|
httppost.setConfig(requestConfig); |
|
|
|
|
|
httppost.addHeader("Content-Type", "application/json"); |
|
|
|
|
|
if (StringUtils.isNotEmpty(json)) { |
|
|
|
|
|
StringEntity se = new StringEntity(json, "utf-8"); |
|
|
|
|
|
httppost.setEntity(se); |
|
|
|
|
|
} |
|
|
|
|
|
return executeToByte(httppost); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("sendGet exception", e); |
|
|
|
|
|
return new Result<byte[]>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private Result<byte[]> executeToByte(HttpRequestBase httpMethod) { |
|
|
private Result<byte[]> executeToByte(HttpRequestBase httpMethod) { |
|
|
CloseableHttpResponse response = null; |
|
|
CloseableHttpResponse response = null; |
|
|
try { |
|
|
try { |
|
@ -334,14 +352,16 @@ public class HttpClientManager { |
|
|
if (response != null && response.getStatusLine() != null) { |
|
|
if (response != null && response.getStatusLine() != null) { |
|
|
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
|
|
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
|
|
InputStream in = response.getEntity().getContent(); |
|
|
InputStream in = response.getEntity().getContent(); |
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream(); |
|
|
byte[] responseContent = IOUtils.toByteArray(in); |
|
|
byte[] buffer = new byte[4096]; |
|
|
// ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
int n = 0; |
|
|
// byte[] buffer = new byte[4096];
|
|
|
while (-1 != (n = in.read(buffer))) { |
|
|
// int n = 0;
|
|
|
output.write(buffer, 0, n); |
|
|
// while (-1 != (n = in.read(buffer))) {
|
|
|
} |
|
|
// output.write(buffer, 0, n);
|
|
|
|
|
|
// }
|
|
|
// return new Result<byte[]>().ok(ArrayUtils.toObject(output.toByteArray()));
|
|
|
// return new Result<byte[]>().ok(ArrayUtils.toObject(output.toByteArray()));
|
|
|
return new Result<byte[]>().ok(output.toByteArray()); |
|
|
// return new Result<byte[]>().ok(output.toByteArray());
|
|
|
|
|
|
return new Result<byte[]>().ok(responseContent); |
|
|
} else { |
|
|
} else { |
|
|
log.warn("execute http method fail,httpStatus:{0}", response.getStatusLine().getStatusCode()); |
|
|
log.warn("execute http method fail,httpStatus:{0}", response.getStatusLine().getStatusCode()); |
|
|
} |
|
|
} |
|
|