简体   繁体   中英

Handle a FormData send from Ajax in a Java File

I am sending a file via Ajax like this:

// Get the selected files from the input.
  var files = fileSelect.files;

// Create a new FormData object.
  var formData = new FormData();

    // Add the file to the request.
    formData.append('photos[]', files[0], files[0].name);


  $.ajax({
      type:"POST",
      url:"URL,
      dataType:"json",
      headers : {"cache-control":"no-cache"},
      timeout : 12000,
      processData: false,
      data:{
          formdata:formData
     }

Now I want to work with the send file in my java class, in a ressource like this:

@PermitAll
@POST
@Path(URL)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> fileHandler(@FormParam("formdata") File formdata){   }

But accessing the file does not work, @FormParam("formdata") File formdata seems to be wrong (or more things?). I want to get access to this file in my ressource class somehow. What am I doing wrong? Maybe someone knows a better solution for this.

You can handle it this way:

I have changed the way FormData is passed. Used id of the form and passed it to create form data:

Javacript:

$.ajax({
            type : 'POST',
            url : rquestURL,
            cache:false,
            processData:false,
            contentType:false,
            data : new FormData($("#"+<your_form_id>)[0])}

Rresource (Added @Consumes(MediaType.MULTIPART_FORM_DATA) annotation):

@Path("/upload")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public ResponseDTO doUpload(FormDataMultiPart multiPartData) {

    // non-file fields
    final String imageId = multiPartData.getField("<your_field_name>").getValue();

    // for file field    
    final FormDataBodyPart filePart = multiPartData.getField("<file_field_name>");
    final ContentDisposition fileDetails = filePart.getContentDisposition();
    final InputStream fileInputStream = filePart.getValueAs(InputStream.class);

    // use the above fields as required
    // file name can be accessed from field "fileDetails"  
    }

When you deal with files, it's not just FormParam, it's FormDataParam.
Also, class File is for entity in your filesystem, not for files inside request. It should be InputStream instead.
So signature should look like this:

@PermitAll
@POST
@Path(URL)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> fileHandler(
    @FormDataParam("formdata") InputStream formdata,
    @FormDataParam("formdata") FormDataContentDisposition fileDetail
){   }

Here you could also see another parameter "FormDataContentDisposition", from it you could take details about data, like filename and size (would be useful, when you will read InputStream).
Note that I wrote this example for JAX-RS. Not sure what library you use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM