如何在使用httpClent java上载时获得进度
有人能告诉我上传文件的进度如何在其他课堂上使用吗 使用任何侦听器或包装器等如何在使用httpClent java上载时获得进度,java,apache,apache-httpclient-4.x,progress,Java,Apache,Apache Httpclient 4.x,Progress,有人能告诉我上传文件的进度如何在其他课堂上使用吗 使用任何侦听器或包装器等 publicstaticsusressedtopostRequest(字符串url、字符串有效负载、映射requestHeaders){ CloseableHttpClient httpClient=null; HttpPost HttpPost=null; CloseableHttpResponse响应=null; SusResponseDTO SusResponseDTO=null; 试一试{ httpClient=
publicstaticsusressedtopostRequest(字符串url、字符串有效负载、映射requestHeaders){
CloseableHttpClient httpClient=null;
HttpPost HttpPost=null;
CloseableHttpResponse响应=null;
SusResponseDTO SusResponseDTO=null;
试一试{
httpClient=HttpClients.createDefault();
httpPost=新的httpPost(url);
最终StringEntity输入=新StringEntity(有效负载,默认编码);
setContentType(JSOM_MIME_类型);
httpPost.setEntity(输入);
对于(最终条目h:requestHeaders.entrySet()){
addHeader(h.getKey(),h.getValue());
}
response=httpClient.execute(httpPost);
如果(response.getStatusLine().getStatusCode()!=200){
抛出新的SusException(失败的\u HTTP\u错误\u代码+响应.getStatusLine().getStatusCode());
}
for(标头:response.getAllHeaders()){
if(header.getName().equals(服务器)和&header.getValue()包含(主服务器)){
susResponseDTO=JsonUtils.jsonStreamToObject(response.getEntity().getContent(),susResponseDTO.class);
打破
}
}
如果(SUSSresponsedTo==null){
HttpEntity=response.getEntity();
String responseString=EntityUtils.toString(实体,默认编码);
MapMap=newhashmap();
map=(map)JsonUtils.jsonToMap(responseString,map);
susResponseDTO=JsonUtils.jsonToObject(map.get(RESPONSE_ENTITY),susResponseDTO.class);
}
}捕获(最终IOE例外){
ExceptionLogger.logException(e,SuSClient.class);
抛出新的SusException(e,SuSClient.class);
}最后{
IOUtils.safeClose(响应);
IOUtils.safeClose(httpClient);
}
返回SUSSresponsedTo;
}
谢谢,我会调查的。可能是重复的
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;
}