WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

RESTful Web Services · Intermediate · question 26 of 100

How do you handle file uploads in RESTful web services?

📕 Buy this interview preparation book: 100 RESTful Web Services questions & answers — PDF + EPUB for $5

Handling file uploads in RESTful web services involves receiving files as part of HTTP requests and processing them on the server side. There are several ways to implement file uploads in a RESTful web service, but one common approach is to use the multipart/form-data encoding type, which is supported by most web browsers and HTTP client libraries.

To handle file uploads in a RESTful web service, the following steps can be taken:

Define an endpoint in the RESTful API to handle file uploads, using the appropriate HTTP method (usually POST or PUT).

Use the @Consumes annotation to specify the media type of the incoming request. For file uploads, this should be multipart/form-data.

Use the @FormDataParam annotation from the org.glassfish.jersey.media.multipart package to define the input parameters that will receive the uploaded files. The @FormDataParam annotation takes a parameter name as an argument, which corresponds to the name of the file input element in the HTML form.

Process the uploaded files on the server side. This can involve saving the file to a file system or database, or performing some other type of processing.

Here is an example of how file uploads can be handled in a RESTful web service using the Jersey framework:

    @Path("/upload")
    public class FileUploadResource {
        @POST
        @Consumes(MediaType.MULTIPART\_FORM\_DATA)
        public Response uploadFile(@FormDataParam("file") InputStream fileInputStream,
        @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
            String fileName = contentDispositionHeader.getFileName();
            
            // Process the uploaded file
            // ...
            
            return Response.status(Response.Status.OK).build();
        }
    }

In this example, the FileUploadResource class defines a single endpoint that handles file uploads. The @POST annotation specifies that this endpoint should handle HTTP POST requests, and the @Consumes annotation specifies that the endpoint should accept requests with multipart/form-data media type.

The uploadFile method takes two input parameters, fileInputStream and contentDispositionHeader, which are annotated with @FormDataParam. The fileInputStream parameter is an input stream that represents the uploaded file, and the contentDispositionHeader parameter contains metadata about the uploaded file, such as the file name and content type.

Once the uploaded file is received, it can be processed on the server side, by saving it to a file system or database, or performing some other type of processing. In this example, the file name is extracted from the contentDispositionHeader object and stored in a variable called fileName. The actual processing of the file is omitted for brevity.

In summary, handling file uploads in RESTful web services involves defining an endpoint that accepts files as part of HTTP requests, using the multipart/form-data encoding type, and processing the uploaded files on the server side. The specific implementation details may vary depending on the framework and libraries used in the project.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic RESTful Web Services interview — then scores it.
📞 Practice RESTful Web Services — free 15 min
📕 Buy this interview preparation book: 100 RESTful Web Services questions & answers — PDF + EPUB for $5

All 100 RESTful Web Services questions · All topics