gpt4 book ai didi

java - 如何从部署在服务器上的 Java Web 服务访问本地文件?

转载 作者:行者123 更新时间:2023-12-01 09:27:17 25 4
gpt4 key购买 nike

我创建了一个 Java Web 服务,可以对所选文件执行指定的功能。我必须将服务部署到服务器,并从我的 Java 代码调用该服务的 url。现在,要执行该功能的文件必须在 post header 中作为参数发送到 Web 服务。如何从服务器访问该文件路径?如果我只是将该文件的路径(例如 C:/folder/file.pdf)作为参数发送给网络服务,它会起作用吗?如果没有,如何实现?

我需要在部署服务之前知道答案。

网络客户端如下:

WebTarget webTarget = clientobj.target("http://localhost:8080/myWebApp/method1/Name1");
MultivaluedMap<String, String> formData = new MultivaluedHashMap<String, String>();
formData.add("methodName", methodName);
formData.add("pdfPath", "C:/folder/a.pdf"); //path to my pdf doc that needs to be sent to the server
formData.add("textFile", "C:/folder/b.txt");
Response response = webTarget.request().post(Entity.form(formData));
String output = response.readEntity(String.class);

网络服务如下:

@POST
@Produces(MediaType.TEXT_PLAIN)
public String createBooking(@PathParam("methodName") String methodName,
@FormParam("pdfPath") String pdfPath,
@FormParam("textFile") String textFile))
{
PdfReader reader = new PdfReader(pdfPath);
}

最佳答案

有两种方法可以做到这一点。

1- If your machine and server are in same network and have shared resources then your server can directly access that file.

2- This is most significant method. You first need to upload that file to your server and then write that file on your server and do what you want with that file.

关于java - 如何从部署在服务器上的 Java Web 服务访问本地文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39739012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com