Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I write a restful web service that accepts a binary file (pdf)
    primarykey
    data
    text
    <p>I'm trying to write a restful web service in java that will take a few string params and a binary file (pdf) param. </p> <p>I understand how to do the strings but I'm getting hung up on the binary file. Any ideas / examples?</p> <p>Here's what I have so far</p> <pre><code>@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(); } </code></pre> <p>Since I've posted this the link that had the answer has been removed, so here is my implementation.</p> <pre><code>@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)) &gt; -1) { out.write(buffer, 0, bytesRead); } in.close(); byte[] byteArray = out.toByteArray(); return byteArray; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload