4

I am working on File Transfer Process with REST Service. I am Using REST Jersey API in Java. I am not doing this process with any html post or via ajax post (ie I am not creating any web application). I Have Just created one REST Http Server, and I am using one REST Client to process file transfer operation. The Web Resource Methods are 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 While starting the REST Server itself I am getting the Exception like not a valid resource methods in testREST.java.... I guess Here Using FormDataParam.. That makes the issue... Could you please anyone help me to point out what wrong with my code.

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
Anand Murugan
  • 223
  • 1
  • 8
  • 16
  • Possible duplicate of this [question](http://stackoverflow.com/questions/1156429/how-do-i-write-a-restful-web-service-that-accepts-a-binary-file-pdf). Have you checked it out? If it doesn't solve your problem, then I can write something for you. – greenkode Mar 14 '13 at 08:09

0 Answers0