500 - 用于servlet泽西休息服务的servlet.init()抛出异常

时间:2014-12-22 13:39:54

标签: java rest servlets

我已经包含了jar文件jersey-archive-1.18,jersey-multipart.jar和mimepull.jar 这是com.mkyong.rest

包中的类文件(UploadFileService.java)
 package com.mkyong.rest;

    import javax.ws.rs.Path;
    //import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import javax.ws.rs.Consumes;


    import javax.ws.rs.POST;
    import javax.ws.rs.core.Response;
    import com.sun.jersey.core.header.FormDataContentDisposition;
    import com.sun.jersey.multipart.FormDataParam;


    @Path("/file")
    public class UploadFileService {

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

        System.out.println("ok");
       /* String uploadedFileLocation = "d://upload/" + fileDetail.getFileName();*/
        String uploadedFileLocation = "d://upload/" + "abc.pdf";

            writeToFile(uploadedInputStream, uploadedFileLocation);

            String output = "File uploaded to : " + uploadedFileLocation;

            return Response.status(200).entity(output).build();

            }

    private void writeToFile(InputStream uploadedInputStream,
                                    String uploadedFileLocation) {
        try {
                OutputStream out = new FileOutputStream(new File(
                uploadedFileLocation));
                int read = 0;
                byte[] bytes = new byte[1024];

                out = new FileOutputStream(new File(uploadedFileLocation));
                while ((read = uploadedInputStream.read(bytes)) != -1) 
                {
                  out.write(bytes, 0, read);
                }
                out.flush();
                out.close();
            } catch (IOException e) {

               e.printStackTrace();
            }
    }

    }

此Upload.html

<form action="rest/file/upload" method="post" enctype="multipart/form-data" style="margin-top:20px;margin-bottom:30px;width:100%;height:60px;">

    <input type="file" name="file"  class="btn btn-info btn-xm" style="float:left;margin-right:6px;margin-bottom:10px;"/>
    <p>Target Upload Path : <input type="text" name="path" /></p>
    <input type="submit" class="btn btn-warning" value="Upload" style="float:left;" id="Map" value="Map" onclick="showImg()"/>

    <img src="image/loading.gif" id="map_img" style="display: none;"/>
    <span id="countdown" class="btn btn-danger" style="float:right;"></span>

 </form>

这是web.xml config

中的servlet映射
<display-name>RESTfulWS</display-name>   
<servlet> 
<servlet-name>Jersey REST Service</servlet-name> 
<servlet-class>com.sun.jersey.spi.container.servlet.UploadFileService</servlet-class> 
<init-param> 
<param-name>com.sun.jersey.config.property.packages</param-name> 
<param-value>com.mkyong.rest</param-value> 
</init-param> 

<load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>Jersey REST Service</servlet-name> 
<url-pattern>/rest/*</url-pattern> 
</servlet-mapping>

你能否告诉我我错在哪里,我收到以下错误:

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1012)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:642)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1555)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)
root cause

com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:491)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:321)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1012)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:642)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1555)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

将以下代码添加到适当位置的.project文件中。

Visibility