File tranfer or file upload using dropwizard framework

2,916 views
Skip to first unread message

leslie Lau

unread,
Feb 13, 2013, 6:56:09 PM2/13/13
to dropwiz...@googlegroups.com
Hi all,

Are there any examples of how to implement file transfer?  I would like to upload an image from client and save it on server.  I did find an example using jersey-multipart but was wondering whether their are othey ways.

Thanx in advance,
Leslie

leslie Lau

unread,
Feb 13, 2013, 8:15:09 PM2/13/13
to dropwiz...@googlegroups.com
I tried using jersey-multipart with DW 0.6.0 and got this error when DW starts up:


ERROR [2013-02-14 01:07:45,061] com.sun.jersey.spi.inject.Errors: The following
errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for method public java.lang.String com.vip.web.resources.PrintConditionResource.uploadFile(java.io.InputStream) at parameter at index 0
WARN  [2013-02-14 01:07:45,070] /: unavailable
! com.sun.jersey.spi.inject.Errors$ErrorMessagesException: null
! at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
! at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
! at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
! at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebAppli
cationImpl.java:765)
! at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebAppli
cationImpl.java:760)


I added a provider to DW using this line:

environment.addProvider(com.sun.jersey.multipart.impl.MultiPartReaderServerSide.class);


And my Resource.java looks like this:


    @POST
    @Path("/logo")
    @Produces(TEXT_PLAIN)
    @Consumes(MULTIPART_FORM_DATA)
    public String uploadFile(@FormDataParam("file") final InputStream stream) {
LOG.info("Enter uploadFile");
        String tempname = UUID.randomUUID().toString();
        final String outputPath = "/tmp" + File.separator + tempname;
        try {
Files.copy(new InputSupplier<InputStream>() {
   @Override
   public InputStream getInput() throws IOException {
       return stream;                
   }
}, new File(outputPath));
LOG.info("Logo saved to: " + outputPath);
return tempname;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        
        return "";
    }


Anyone knows what that error means?

leslie Lau

unread,
Feb 14, 2013, 1:33:08 PM2/14/13
to dropwiz...@googlegroups.com
Now, I've got a different error......


127.0.0.1 - - [14/Feb/2013:18:19:19 +0000] "POST /uploadattachment HTTP/1.1" 415 1393 -1360796149836 -1360796149836
ERROR [2013-02-14 18:19:23,907] com.sun.jersey.spi.container.ContainerRequest: A
 message body reader for Java class com.sun.jersey.multipart.FormDataMultiPart,
and Java type class com.sun.jersey.multipart.FormDataMultiPart, and MIME media t
ype multipart/form-data; boundary=----------------------------b756c98ff995 was n
ot 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.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.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.fasterxml.jackson.jaxrs.json.JacksonJsonProvider


To test my uploadFile API, I use curl to simulate a POST:

curl -F file=@"C:\\my.png" http://localhost:8080/uploadattachment


Anyone knows what that error registered message body reader is all about?

leslie Lau

unread,
Feb 14, 2013, 2:18:31 PM2/14/13
to dropwiz...@googlegroups.com
Whew....all working now....

For those who would like to know, I needed to include this jar:

mimepull-1.6.jar

I'm now able to upload file to my dropwizard service.

Leslie
Reply all
Reply to author
Forward
0 new messages