File 黑客使用RESTAPI通过HTTP将文件从Java后端上传到远程服务器。
我的文件位于我的计算机上的某个位置,比如C://users//abc.txt,我想编写一个java程序,使用RESTAPI通过HTTP传输该文件。我使用MockHttpServelet Request创建请求,但不知何故我无法传输文件使用:File 黑客使用RESTAPI通过HTTP将文件从Java后端上传到远程服务器。,file,transfer,File,Transfer,我的文件位于我的计算机上的某个位置,比如C://users//abc.txt,我想编写一个java程序,使用RESTAPI通过HTTP传输该文件。我使用MockHttpServelet Request创建请求,但不知何故我无法传输文件使用: String url = "http://localhost:8080/upload"; // Replace with your target 'REST API' url String filePath = "C://users//abc.txt";
String url = "http://localhost:8080/upload"; // Replace with your target 'REST API' url
String filePath = "C://users//abc.txt";
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
FileEntity entity = new FileEntity(new File(filePath), ContentType.TEXT_PLAIN);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse.getStatusLine().getStatusCode()); // Check HTTP code
} finally {
httpClient.close();
}
String url = "http://localhost:8080/upload"; // Replace with your target 'REST API' url
String filePath = "C://users//abc.txt";
String username = "username"; // Replace with your username
String password = "password"; // Replace with your password
RequestConfig requestConfig =
RequestConfig.custom().
setAuthenticationEnable(true).
build();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredential(username, password));
CloseableHttpClient httpClient =
HttpClients.custom().
setDefaultRequestConfig(requestConfig).
setDefaultCredentialsProvider(credentialsProvider).
build();
try {
HttpPost httpPost = new HttpPost(url);
FileEntity entity = new FileEntity(new File(filePath), ContentType.TEXT_PLAIN);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse.getStatusLine().getStatusCode()); // Check HTTP code
} finally {
httpClient.close();
}
具有身份验证:
String url = "http://localhost:8080/upload"; // Replace with your target 'REST API' url
String filePath = "C://users//abc.txt";
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
FileEntity entity = new FileEntity(new File(filePath), ContentType.TEXT_PLAIN);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse.getStatusLine().getStatusCode()); // Check HTTP code
} finally {
httpClient.close();
}
String url = "http://localhost:8080/upload"; // Replace with your target 'REST API' url
String filePath = "C://users//abc.txt";
String username = "username"; // Replace with your username
String password = "password"; // Replace with your password
RequestConfig requestConfig =
RequestConfig.custom().
setAuthenticationEnable(true).
build();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredential(username, password));
CloseableHttpClient httpClient =
HttpClients.custom().
setDefaultRequestConfig(requestConfig).
setDefaultCredentialsProvider(credentialsProvider).
build();
try {
HttpPost httpPost = new HttpPost(url);
FileEntity entity = new FileEntity(new File(filePath), ContentType.TEXT_PLAIN);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse.getStatusLine().getStatusCode()); // Check HTTP code
} finally {
httpClient.close();
}
不知道RESTAPI,但我已经在服务器上上传了文件,MySql作为后端。不确定您使用的是什么,是MongoDB吗?Milind,独立的Java应用程序正在后端运行。我有一个使用RESTAPI将文件从我的目录传输到某个服务器的函数。服务器接受RESTAPI重新请求。我不想使用任何像scp这样的协议进行文件传输。服务器将仅通过Rest API接受请求,但必须有一些数据库来存储您的文件?/File存储的是我机器上的某个目录,而不是数据库上的某个目录。现在,文件位置是经过加密的,不存储在数据库中(比如C://Users/abc.txt),谢谢Greg。您知道需要在哪里指定会话/用户凭据以便服务器允许访问它吗?身份验证使用是另一个问题。使用HttpClients.custom().setDefaultRequestConfig(requestConfig).setDefaultCredentialsProvider(credentialsProvider)。它不会影响性能吗?我的意思是,当打开文件进行传输时,JVM会将文件加载到内存中。如果是大型的,我相信会有性能问题。你知道如何避免吗?你可以自己检查以确保文件不会加载到内存中。