无法部署maven restful上传文件

时间:2017-10-28 11:57:44

标签: java rest maven jersey glassfish

package Restful.Demo.UploadFile;

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.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

@Path("UploadFile")
public class Upload {

    @GET
    @Path("/upload1")
    public String hello()
    {
        return "hello";
    }

    @POST 
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)// use to upload file
    public Response UploadFile(@FormDataParam("file") InputStream inputfile,
                              @FormDataParam("file") FormDataContentDisposition filemetadetail) 
    {

            int statuscode=200;
            String fileLocation="e://Restful/"+filemetadetail.getFileName();
            writeFile(inputfile, fileLocation);
            String output="File uploaded to "+fileLocation;
            return Response.status(statuscode).entity(output).build(); 

    }
    /*
     * @param inputFile the file you want to upload
     * @param fileLocation where  file will be uploaded 
     * @return 0 - success 
     * @throws IOException
     */
    private void writeFile(InputStream inputFile,String fileLocation) 
    {
        OutputStream output=null;
        try
        {
            output=new FileOutputStream(new File(fileLocation));
            int read=0;
            byte[] bytes=new byte[1024];
            //read 
            while((read=inputFile.read(bytes))!=-1)
                output.write(bytes, 0, read);
            output.flush();
            output.close();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }

    }
}

您好。 我是Restful webserver中的彼得和新手。 上面是我使用restful api的uploadfile代码 - 在Glassfish服务器上运行Jersey 我试图将战争发布到glassfish服务器时,我收到了这个错误

  

java.lang.Exception:deploy is failing =具有名称的应用程序   [UploadFile]未部署在   oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.publishDeployedDirectory(GlassFishServerBehaviour.java:603)     在   oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.publishModuleForGlassFishV3(GlassFishServerBehaviour.java:830)     在   oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.publishModule(GlassFishServerBehaviour.java:790)     在   org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModule(ServerBehaviourDelegate.java:1091)     在   org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModules(ServerBehaviourDelegate.java:1183)     在   org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:987)     在   org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:774)     在   org.eclipse.wst.server.core.internal.Server.publishImpl(Server.java:3182)     在   org.eclipse.wst.server.core.internal.Server $ PublishJob.run(Server.java:355)     在org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

为了使你的资源类能够工作,你需要做一些事情。

  1. 要么提供Application子类,要么提供web.xml部署描述符。
  2. 为了能够使用多部分模块,您需要在服务器代码上启用此功能。以下是documentation page
  3. 的摘录
      

    9.3.1.2。注册

         

    在客户端/服务器代码中使用jersey-media-multipart模块的功能之前,您需要注册MultiPartFeature。

    也就是说,我在示例中添加了以下web.xml,我可以在Glassfish 4上部署应用程序:

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    
    <display-name>Archetype Created Web Application</display-name>
    
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>jersey.config.server.provider.packages</param-name>
                <!-- please see modified package name -->
                <param-value>restful.demo.uploadfile</param-value> 
            </init-param>
            <init-param>
                <param-name>jersey.config.server.provider.classnames</param-name>
                <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>jersey-serlvet</servlet-name>
    
            <!-- you can choose your favorite name here!! -->
            <url-pattern>/rest/*</url-pattern>    
        </servlet-mapping>
    
    </web-app>
    

    注意:

    • 请参阅上面评论的部分,并尝试相应地修改您的代码。

    • 您应该了解Java中的一些命名约定(有关详细信息,请参阅this文档):

      1. 在Java包名称中使用小写字母
      2. 方法名称以小写字母
      3. 开头