1

I have REST API in java that take image file and save on the server i want to return the path of that uploaded image in XML form but don't know how to do it.Currently it returns the response as a string in the browser.

Here is my code.

package com.javacodegeeks.enterprise.rest.jersey;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("/files")
public class JerseyFileUpload {

    private static final String SERVER_UPLOAD_LOCATION_FOLDER = "/home/hassan/Downloads/";

    /**
     * Upload a File
     */

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
            @FormDataParam("file") InputStream fileInputStream,
            @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {

        String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();

        // save the file to the server
        saveFile(fileInputStream, filePath);

        String output = "File saved to server location : " + filePath;

        return Response.status(200).entity(output).build();

    }

    // save uploaded file to a defined location on the server
    private void saveFile(InputStream uploadedInputStream,
            String serverLocation) {

        try {
            OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            outpuStream = new FileOutputStream(new File(serverLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                outpuStream.write(bytes, 0, read);
            }
            outpuStream.flush();
            outpuStream.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}
3
  • This doesn't solve your purported problem, but I would expect that the location of the image file would be returned in the Location header of the response. Commented Jan 27, 2016 at 14:02
  • output is a string. You need to return an entity (instance) of a class that is annotated with @XmlElement Commented Jan 27, 2016 at 14:02
  • Can i use Produces annotation as well in this same class because Consumes annotation is also here can i use both at same time.@RobAu how to use @XmlElement as i am pretty new. Commented Jan 27, 2016 at 14:08

1 Answer 1

3

If you want to return xml from Rest, try to create Object with some fields. and Object and field will have @XmlRootElement @XmlElement and put @Produces("application/xml") on top of method signature.

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces("application/xml")
    public Response uploadFile(...){
         //body
   }

also you can use @produces(MediaType.APPLICATION_XML) instead of @Produces("application/xml"). both are same.

Sign up to request clarification or add additional context in comments.

3 Comments

will i have to create object of that class or another class.
Thanks @Tipu It really helps me and solves my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.