Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a restful web service that accepts a binary file (pdf)

I'm trying to write a restful web service in java that will take a few string params and a binary file (pdf) param.

I understand how to do the strings but I'm getting hung up on the binary file. Any ideas / examples?

Here's what I have so far

@GET
@ConsumeMime("multipart/form-data")
@ProduceMime("text/plain")
@Path("submit/{client_id}/{doc_id}/{html}/{password}")
public Response submit(@PathParam("client_id") String clientID,
                   @PathParam("doc_id") String docID,
                   @PathParam("html") String html,
                   @PathParam("password") String password,
                   @PathParam("pdf") File pdf) {
  return Response.ok("true").build();
}

Since I've posted this the link that had the answer has been removed, so here is my implementation.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path("submit")
public Response submit(@FormDataParam("clientID") String clientID,
                   @FormDataParam("html") String html,
                   @FormDataParam("pdf") InputStream pdfStream) {

    try {
        byte[] pdfByteArray = DocUtils.convertInputStreamToByteArrary(pdfStream);
    } catch (Exception ex) {
        return Response.status(600).entity(ex.getMessage()).build();
    }
}


...

public static byte[] convertInputStreamToByteArrary(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    final int BUF_SIZE = 1024;
    byte[] buffer = new byte[BUF_SIZE];
    int bytesRead = -1;
    while ((bytesRead = in.read(buffer)) > -1) {
        out.write(buffer, 0, bytesRead);
    }
    in.close();
    byte[] byteArray = out.toByteArray();
    return byteArray;
}
like image 947
Preston Avatar asked Jul 20 '09 23:07

Preston


People also ask

How do you send binary data to the postman?

First, set the body to "binary": Select "binary" to have Postman send binary data. Now click "Select File" in Postman to attach a file: Attach a file to upload with the "Select File" button.

Is PDF a binary file?

File format. A PDF file is organized using ASCII characters, except for certain elements that may have binary content. The file starts with a header containing a magic number (as a readable string) and the version of the format, for example %PDF-1.7 .

Is PDF a binary or text file?

PDF files are either 8-bit binary files or 7-bit ASCII text files (using ASCII-85 encoding). Every line in a PDF can contain up to 255 characters.


1 Answers

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path("submit")
public Response submit(@FormDataParam("clientID") String clientID,
                   @FormDataParam("html") String html,
                   @FormDataParam("pdf") InputStream pdfStream) {

    try {
        byte[] pdfByteArray = DocUtils.convertInputStreamToByteArrary(pdfStream);
    } catch (Exception ex) {
        return Response.status(600).entity(ex.getMessage()).build();
    }
}


...

public static byte[] convertInputStreamToByteArrary(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    final int BUF_SIZE = 1024;
    byte[] buffer = new byte[BUF_SIZE];
    int bytesRead = -1;
    while ((bytesRead = in.read(buffer)) > -1) {
        out.write(buffer, 0, bytesRead);
    }
    in.close();
    byte[] byteArray = out.toByteArray();
    return byteArray;
}
like image 152
Preston Avatar answered Sep 16 '22 11:09

Preston