简体   繁体   中英

How can I access a local file from a Java webservice deployed on a server?

I have created a Java webservice that performs a specified function on a chosen file. I will have to deploy the service to the server and will be calling the url to thr service from my Java code. Now, the file on which the function is to be performed has to be sent in post header as a parameter to the webservice. How can I access this file path from the server? If I simply send the path to that file (eg C:/folder/file.pdf) as a parameter to the webservice, will it work? If not, how can that be achieved?

I need to know the answer before the service is deployed.

The webclient is as follows:

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);

and the webservice is as follows:

@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);
}

There are two approach to do that.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM