我有一个本地文件C:\TEMP\296A84G5D98S.pdf
,我想将其上传到远程HTTP服务器,用户名为:adminuser
密码:adminP@ssw0rd
远程服务器本地路径位置:C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\FILES
和服务器URL是:http://fileserver:9090/files
我已经实现了这个方法,但这不起作用而且没有给我错误。也许它没有完成或其他什么。
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("fileserver", 9090),
new UsernamePasswordCredentials("adminuser", "adminP@ssw0rd"));
//CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
try {
HttpPost httpPost = new HttpPost("http://fileserver:9090/FILES/");
FileBody fileBody = new FileBody(new File(CompleteName));
HttpEntity httpEntity = MultipartEntityBuilder.create().addPart("file", fileBody).build();
CloseableHttpResponse response = httpClient.execute(httpPost);
System.err.println("" + response.getEntity());
try {
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
}
EntityUtils.consume(responseEntity);
} catch (Exception responseEx) {
responseEx.printStackTrace();
} finally {
response.close();
}
httpClient.close();
} catch (Exception ex) {
ex.printStackTrace();
}