5

I use following REST service (from this tutorial) to do file uploads from various number of clients to my GlassFish server, using jersey multipart implementation:

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("/fileupload")
public class UploadFileService {

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

        String uploadedFileLocation = "c://uploadedFiles/" + fileDetail.getFileName();

        // save it
        saveToFile(uploadedInputStream, uploadedFileLocation);

        String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;

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

    // save uploaded file to new location
    private void saveToFile(InputStream uploadedInputStream,
        String uploadedFileLocation) {

        try {
            OutputStream out = null;
            int read = 0;
            byte[] bytes = new byte[1024];

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

This code works fine for me but I noticed following:

  1. first the file is uploaded somewhere in the server
  2. the method uploadFile(...) is triggered with an InputStream to the uploaded file, so I can make a copy of it in desired location using saveToFile(...)

My questions are:

  • where is the file (see (1) above) initially stored?
  • How to delete the uploaded file to free resources?

UPDATE 1

Attached the InputStream dump: enter image description here

The strange thing here - .tmp file from screenshot is 0 bytes in size! The .tmp is deleted after out.close()

3
  • Hello can you please share the client side implementation, i want to know how to send file to this service? thanks Commented Aug 14, 2015 at 2:09
  • @filip_i did the above code works for you? Since it is not working for me. Can you please help me Commented Feb 17, 2016 at 8:57
  • @Divya can you please share your exception or some description of what you do and what is not working? Commented Feb 18, 2016 at 15:42

1 Answer 1

4

The uploaded file is probably either kept in memory (meaning it will be freed when the inputstream is clean up by the gc) or it is stored in the system default temp-folder. (Probably the same folder returned by System.getProperty("java.io.tmpdir"), which means that it is cleaned up whenever you clean temporary files from your filesystem.

The exact location is dependent upon the framework which handles the restservices for you. In your case it appears to be jersey.

I don't know where jersey saves these files. You could try looking at the provided inputstream to see what type it is and where it's stored.

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

Comments

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.