Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@MultipartForm How to get the original file name?

I am using jboss's rest-easy multipart provider for importing a file. I read here http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Content_Marshalling_Providers.html#multipartform_annotation regarding @MultipartForm because I can exactly map it with my POJO.

Below is my POJO

public class SoftwarePackageForm {

    @FormParam("softwarePackage")
    private File file;

    private String contentDisposition;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getContentDisposition() {
        return contentDisposition;
    }

    public void setContentDisposition(String contentDisposition) {
        this.contentDisposition = contentDisposition;
    }
}

Then I got the file object and printed its absolute path and it returned a file name of type file. The extension and uploaded file name are lost. My client is trying to upload a archive file(zip,tar,z)

I need this information at the server side so that I can apply the un-archive program properly.

The original file name is sent to the server in content-disposition header.

How can I get this information? Or atleast how can I say jboss to save the file with the uploaded file name and extension? Is it configurable from my application?

like image 793
Krishna Chaitanya Avatar asked Oct 13 '14 05:10

Krishna Chaitanya


People also ask

What is a multi part file?

Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file uploads and for transferring data of several types in a single request (for example, a file along with a JSON object).

What is MultipartFile file in Java?

public interface MultipartFile extends InputStreamSource. A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired ...

What is multipart in spring boot?

Introduction. In this tutorial, we'll focus on various mechanisms for sending multipart requests in Spring Boot. Multipart requests consist of sending data of many different types separated by a boundary as part of a single HTTP method call.


Video Answer


2 Answers

After looking around a bit for Resteasy examples including this one, it seems like there is no way to retrieve the original filename and extension information when using a POJO class with the @MultipartForm annotation.

The examples I have seen so far retrieve the filename from the Content-Disposition header from the "file" part of the submitted multiparts form data via HTTP POST, which essentially, looks something like:

Content-Disposition: form-data; name="file"; filename="your_file.zip"
Content-Type: application/zip

You will have to update your file upload REST service class to extract this header like this:

@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response uploadFile(MultipartFormDataInput input) {

  String fileName = "";
  Map<String, List<InputPart>> formParts = input.getFormDataMap();

  List<InputPart> inPart = formParts.get("file"); // "file" should match the name attribute of your HTML file input 
  for (InputPart inputPart : inPart) {
    try {
      // Retrieve headers, read the Content-Disposition header to obtain the original name of the file
      MultivaluedMap<String, String> headers = inputPart.getHeaders();
      String[] contentDispositionHeader = headers.getFirst("Content-Disposition").split(";");
      for (String name : contentDispositionHeader) {
        if ((name.trim().startsWith("filename"))) {
          String[] tmp = name.split("=");
          fileName = tmp[1].trim().replaceAll("\"","");          
        }
      }

      // Handle the body of that part with an InputStream
      InputStream istream = inputPart.getBody(InputStream.class,null);

      /* ..etc.. */
      } 
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  String msgOutput = "Successfully uploaded file " + filename;
  return Response.status(200).entity(msgOutput).build();
}

Hope this helps.

like image 84
ivan.sim Avatar answered Sep 22 '22 23:09

ivan.sim


You could use @PartFilename but unfortunately this is currently only used for writing forms, not reading forms: RESTEASY-1069.

Till this issue is fixed you could use MultipartFormDataInput as parameter for your resource method.

like image 28
lefloh Avatar answered Sep 24 '22 23:09

lefloh