|
|
@ -29,6 +29,7 @@ import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
import javax.crypto.Mac; |
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
@ -307,6 +308,65 @@ public class HttpClientManager { |
|
|
|
return new Result<File>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取下载文件的字节数组 |
|
|
|
* @param url |
|
|
|
* @param params |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public Result<byte[]> getDownloadFilebytes(String url, Map<String, Object> params) { |
|
|
|
|
|
|
|
try { |
|
|
|
URIBuilder builder = new URIBuilder(url); |
|
|
|
if (!CollectionUtils.isEmpty(params)) { |
|
|
|
Set<String> set = params.keySet(); |
|
|
|
for (String key : set) { |
|
|
|
builder.setParameter(key, params.get(key) == null ? "" : String.valueOf(params.get(key))); |
|
|
|
} |
|
|
|
} |
|
|
|
HttpGet httpGet = new HttpGet(builder.build()); |
|
|
|
httpGet.setConfig(requestConfig); |
|
|
|
return executeTobyte(httpGet); |
|
|
|
} 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) { |
|
|
|
CloseableHttpResponse response = null; |
|
|
|
try { |
|
|
|
response = httpclient.execute(httpMethod); |
|
|
|
log.debug("http send response:{}", JSON.toJSONString(response)); |
|
|
|
if (response != null && response.getStatusLine() != null) { |
|
|
|
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
|
|
|
InputStream in = response.getEntity().getContent(); |
|
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream(); |
|
|
|
byte[] buffer = new byte[4096]; |
|
|
|
int n = 0; |
|
|
|
while (-1 != (n = in.read(buffer))) { |
|
|
|
output.write(buffer, 0, n); |
|
|
|
} |
|
|
|
return new Result<byte[]>().ok(output.toByteArray()); |
|
|
|
} else { |
|
|
|
log.warn("execute http method fail,httpStatus:{0}", response.getStatusLine().getStatusCode()); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("execute exception", e); |
|
|
|
return new Result<byte[]>().error(EpmetErrorCode.SERVER_ERROR.getCode(), e.getMessage()); |
|
|
|
} finally { |
|
|
|
httpMethod.releaseConnection(); |
|
|
|
try { |
|
|
|
if (response != null) { |
|
|
|
response.close(); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
} |
|
|
|
} |
|
|
|
return new Result<byte[]>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|