I am working on a file transfer process using a REST service. I am using the Jersey REST API in Java. I do not do this process with any html post or through ajax post (i.e. I do not create any web application). I just created one REST Http Server, and I use one REST client to handle file transfers. Web resource methods are located in a separate class file.
testREST.java
@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) { String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName(); // save it writeToFile(uploadedInputStream, uploadedFileLocation); String output = "File uploaded to : " + uploadedFileLocation; return Response.status(200).entity(output).build(); }
RESTClient.java
try { final ClientConfig config = new DefaultClientConfig(); final Client client = Client.create(config); final WebResource resource = client.resource(REST_URI); File f = new File("D:/test/Doc1.rar"); FileInputStream fs = new FileInputStream(f); byte[] con= new byte[(int)f.length()]; fs.read(con); FormDataMultiPart form = new FormDataMultiPart(); FormDataBodyPart fdp = new FormDataBodyPart("content", MediaType.MULTIPART_FORM_DATA); form.bodyPart(fdp); final String response = resource.path("test").path("transfer").type(MediaType.MULTIPART_FORM_DATA).post(String.class, form); } catch (Exception ex) { System.out.println("Exception Occcured"); ex.printStackTrace(); }
But when I start the REST server, I get an exception as invalid resource methods in testREST.java .... I assume that FormDataParam is used here .. This makes the problem ... Could you please help me indicate what is wrong with my code.
source share