File upload services using jersey in tomcat withou

2020-07-22 09:50发布

问题:

I want to upload images into server using restful jersey web services.I have included jersey-multipart-1.9.jar,jersey-bundle-1.14.jar and asm-3.3.1.jar jar files and I am not using Maven.
Below code snippet for upload function.

@POST
@Path("/uploadImage")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream fileInputStream,
                           @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) { String filePath = SERVER_UPLOAD_LOCATION_FOLDER    + contentDispositionHeader.getFileName();
    saveFile(fileInputStream, filePath);//method to save the file.
    String output = "File saved to server location : " + filePath;
    return Response.status(200).entity(output).build();

}

But i am getting following error when i deploy or run it in apache tomcat 7.

SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.sec.samsung.FileUpload.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at parameter at index 0
SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.sec.samsung.FileUpload.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at parameter at index 1
SEVERE: Method, public javax.ws.rs.core.Response com.sec.samsung.FileUpload.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), annotated with POST of resource, class com.sec.samsung.FileUpload, is not recognized as valid resource method.
SEVERE: Servlet /uploadImage threw load() exception
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
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(WebApplicationImpl.java:765)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:760)
at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:489)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Can anyone help me without maven support? many people have asked same question but solution is available using maven, but i am not working on maven project.

回答1:

I tried with the similar code

    //code
@POST
@Path("/uploadImage")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream uploadedStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {
    String fileName = fileDetail.getFileName();
    saveToFile(uploadedStream,fileName);
    String output = "File saved to server location : " + fileName;
    return Response.status(200).entity(output).build();
}

with bellow jar files in (Tomcat 7) & it is working fine

   jersey-multipart-1.18.1.jar
   mimepull-1.9.3.jar
   jersey-core-1.18.1.jar
   jersey-servlet-1.18.1.jar
   jersey-server-1.18.1.jar