简体   繁体   中英

File upload with ExtJS and Jersey

I have an application based on ExtJS and I want to upload a file, an image, via Restful with Jersey, but I can't do it, is that possible?. This is part of my code:

View:

.
.
.
items: [{
  xtype: 'filefield',
  name: 'iconBmp',
  fieldLabel: this.i18nColIconBmp,
  enforceMaxLength : true,
  labelWidth: 100,
  msgTarget: 'side',
  anchor: '100%',
  buttonText: 'Exam'
}],
buttons: [{
  text: 'Upload',
  handler: function() {
    var form = this.up('form').getForm();
    if (form.isValid()) {
      form.submit({
        url: 'icon/upload',
        waitMsg: 'Uploading...',
        success: function(fp, o) {
          Ext.Msg.alert('Success', o.result.file + ' has been uploaded.');
        }
      });
    }
  }
}]

Java:

@Path("icon")
public class CatIconRest {

  @POST
  @Path("/upload")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  public Response uploadFIle(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) {

    String string = fileDetail.getFileName();
    System.out.println(string );
    // TODO:
    return null;
  }
}

The thing is that I never get to the Java class, ExtJS form doesn't send anything, What I have to change/add??

If you need more code, just ask.

Thanks in advance!

EDIT: I'm testing it with a chrome extension called Postman, and these are the configuration of the request:

Postman Test:

Accept: application/json
Content-type: multipart/form-data
Cache-Control: no-cache
Url: http://localhost/app/icon/upload
Method: POST
File: Mine.doc

I added some jars to the project, jersey-multipart-1.17.1.jar and, mimepull-1.9.3.jar, but I'm unable to test it with the extension, and this is what I get:

A message body reader for Java class com.sun.jersey.core.header.FormDataContentDisposition, and Java type class com.sun.jersey.core.header.FormDataContentDisposition, and MIME media type multipart/form-data was not found.
The registered message body readers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

Greetings.

Apparently had a dependency problem in the pom.xml, and had misconfigured the method, I leave it to someone else if you could help:

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_JSON })
public ServiceResponse uploadFIle(FormDataMultiPart form) {
    byte[] fileContents = form.getField("iconBmp").getValueAs(byte[].class);
    String uploadedfilename = form.getField("iconBmp").getContentDisposition().getFileName();
    String description = form.getField("description").getValue();
    String fileName = form.getField("fileName").getValue();
    //String id = form.getField("id").getValue();

    // TODO:
    //catIconBean.uploadIcon(fileContents);

    ServiceResponse sr = new ServiceResponse();
    sr.httpResponse = true;
    return sr;
}

Greetings.

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