David Martinelli
David Martinelli

Reputation: 233

Jersey Accept a File parameter and a String using POST

I'm developing a rest server using Jersey. I need to make an API that upload an image into a folder on a server. My API must contain 2 parameters. A string parameter and a File parameter.

@POST
@Path("path")
@public Response uploadImage(@FormParam("name") String name, @FormDataParam("imagestream") InputStream imageStream)

If I send 2 parameters from my client app, I can't receive both attributes.

My client-side code is the following:

private static void instantiateConnection(String urlString, byte[] params)
{
    HttpURLConnection conn = null;

    try
    {
        URL url = new URL(urlString);

        conn = (HttpURLConnection) url.openConnection();

        outStream(conn, params);

        int responseCode = conn.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK)
        {
            InputStream instream = conn.getInputStream();

            instream.close();
        }

    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        conn.disconnect();
    }
}

private static void outStream(HttpURLConnection conn, byte[] param)
{
    String name = new String("name");

    conn.addRequestProperty("Content-Length", "" + (param.length + name.length()));
    conn.addRequestProperty("Content-Type", "multipart/mixed");

    try
    {
        conn.setRequestMethod("POST");
        conn.setUseCaches(false);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setReadTimeout(15000);


        // Send request
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.write(name.getBytes());
        wr.write(param);
        wr.flush();
        wr.close();
    }
    catch (ProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return;
}

Where am I wrong?

Upvotes: 4

Views: 3810

Answers (1)

greenkode
greenkode

Reputation: 3996

Here's a snippet of code I used.

@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response newFile(@FormDataParam("file") InputStream uploadInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("description") String description) {

    String uploadFileLocation = "C:/upload/documents/";

    OutputStream out = null;

    int read = 0;
    byte[] bytes = new byte[1024];

    File directory = new File(uploadFileLocation);
    if(!directory.exists()){
        directory.mkdirs();
    }
    out = new FileOutputStream(new File(directory, fileDetail.getFileName()));
    while ((read = uploadInputStream.read(bytes)) != -1) {
        out.write(bytes, 0, read);
    }

    out.flush();
    out.close();

//Return Response
...
}

Upvotes: 1

Related Questions