java - 如何在使用 httpClent java 上传时取得进展
问题描述
有人可以告诉我如何获得上传文件进度并在其他课程中使用它
使用任何监听器或包装器等
public static SusResponseDTO postRequest( String url, String payload, Map< String, String > requestHeaders ) {
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
CloseableHttpResponse response = null;
SusResponseDTO susResponseDTO = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost( url );
final StringEntity input = new StringEntity( payload, DEFAULT_ENCODING );
input.setContentType( JSOM_MIME_TYPE );
httpPost.setEntity( input );
for ( final Entry< String, String > h : requestHeaders.entrySet() ) {
httpPost.addHeader( h.getKey(), h.getValue() );
}
response = httpClient.execute( httpPost );
if ( response.getStatusLine().getStatusCode() != 200 ) {
throw new SusException( FAILED_HTTP_ERROR_CODE + response.getStatusLine().getStatusCode() );
}
for ( Header header : response.getAllHeaders() ) {
if ( header.getName().equals( SERVER ) && header.getValue().contains( MASTER_SERVER ) ) {
susResponseDTO = JsonUtils.jsonStreamToObject( response.getEntity().getContent(), SusResponseDTO.class );
break;
}
}
if ( susResponseDTO == null ) {
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString( entity, DEFAULT_ENCODING );
Map< String, String > map = new HashMap<>();
map = ( Map< String, String > ) JsonUtils.jsonToMap( responseString, map );
susResponseDTO = JsonUtils.jsonToObject( map.get( RESPONSE_ENTITY ), SusResponseDTO.class );
}
} catch ( final IOException e ) {
ExceptionLogger.logException( e, SuSClient.class );
throw new SusException( e, SuSClient.class );
} finally {
IOUtils.safeClose( response );
IOUtils.safeClose( httpClient );
}
return susResponseDTO;
}
解决方案
推荐阅读
- android - 当半径超过 25 时,BlurTransformation 不起作用
- c# - 使用 C# 通过注册表设置更新 PATH 环境变量会导致路径不再有效
- javascript - 导入第三方js库
- javascript - 解构 JavaScript 对象时如何只选择几个字段?
- jquery - 单击 div 外部或单击后代时隐藏 div
- kubernetes - 在 sts 副本中使用不同的 Secret
- opencl - 是否可以将异步回调/延续附加到 SYCL 内核?
- python - 检查 shell 命令是否已成功执行的最佳方法
- arrays - 选择我创建的数组时下标超出范围
- gremlin - Gremlin select multiple vertices 给出的输出没有具有空值的属性