Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 错误404:Tomcat Eclipse Web服务_Java_Eclipse_Rest_Tomcat_Glassfish - Fatal编程技术网

Java 错误404:Tomcat Eclipse Web服务

Java 错误404:Tomcat Eclipse Web服务,java,eclipse,rest,tomcat,glassfish,Java,Eclipse,Rest,Tomcat,Glassfish,这可能看起来有点重复,但我发誓我已经看过了关于堆栈溢出的所有答案,似乎没有什么对我有用。 我正在使用EclipseNeon中的Jersey制作一个文件上传web服务 服务器:我的PC上有Tomcat8.5(它是C语言的,我可以将WAR文件放在webapps中并运行它),我的eclipse上有GlassFish服务器4 当我在任何服务器上运行我的应用程序时,会出现错误404 not found: 我的包资源管理器如下所示: 早些时候,我将org.glassfish servlet容器名称放在w

这可能看起来有点重复,但我发誓我已经看过了关于堆栈溢出的所有答案,似乎没有什么对我有用。 我正在使用EclipseNeon中的Jersey制作一个文件上传web服务

服务器:我的PC上有Tomcat8.5(它是C语言的,我可以将WAR文件放在webapps中并运行它),我的eclipse上有GlassFish服务器4

当我在任何服务器上运行我的应用程序时,会出现错误404 not found:

我的包资源管理器如下所示:

早些时候,我将org.glassfish servlet容器名称放在web.xml中,因此有时它会以错误500结束,这是Classnot Found,我理解这是因为glassfish引用在jersey 2.x中有效

老实说,我不明白我做错了什么。我现在真的要把头发拔出来了。我已经试着做了一个星期了。有人请把我从精神崩溃中救出来。我可能是一个Java noob,但几天后我就要提交申请了,我的毕业典礼就岌岌可危了。救命啊

web.xml

文件上传
Jersey RESTful应用程序
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
com.tutorialspoint
Jersey RESTful应用程序
/休息/*

看起来您的项目中有
jersey
版本不匹配。我建议按照以下步骤进行操作

删除所有手动添加的引用库。我们将使用maven来管理依赖关系

使用下面的
pom.xml
(根据需要添加其他依赖项)

删除您的资源文件并在下面添加一个

package com.quratulaind.us3;

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.Produces; 
import javax.ws.rs.core.MediaType;

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

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("cv")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String getIt() {
         return "Got it!";
     }

     @POST
     @Path("/upload")
     @Consumes(MediaType.MULTIPART_FORM_DATA)
     public String uploadCV(
         @FormDataParam("file") InputStream uploadedInputStream,
         @FormDataParam("file") FormDataContentDisposition fileDetail) 
     {
         //saving the file object to the local disk
         saveToDisk(uploadedInputStream, fileDetail);
         return "File Uploaded Successfully";
     }//function uploadcv ends

     //Fuction to save uploaded file to disk
     private void saveToDisk(InputStream uploadedInputStream, 
         FormDataContentDisposition fileDetail) {
         String uploadedFileLocation = "D:\\University Study Stuff\\FYP\\Uploaded CVs For Project"+fileDetail.getFileName();
         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();

          }//try ends
          catch(IOException e)
          {e.printStackTrace();}
      }
  }
完成这些更改后,使用
mvn包创建war包,并部署/复制到
${TOMCAT\u HOME\webapps

检查样本资源是否正常工作

http://localhost:8080/{CONTEXT_PATH}/rest/cv

如果以上工作正常,请尝试上传资源的方法


另外,我没有测试你的上传代码,这是你的作业,你应该完成。

看起来你的项目中有
jersey
版本不匹配。我建议按照以下步骤进行操作

删除所有手动添加的引用库。我们将使用maven管理依赖项

使用下面的
pom.xml
(根据需要添加其他依赖项)

删除您的资源文件并在下面添加一个

package com.quratulaind.us3;

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.Produces; 
import javax.ws.rs.core.MediaType;

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

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("cv")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String getIt() {
         return "Got it!";
     }

     @POST
     @Path("/upload")
     @Consumes(MediaType.MULTIPART_FORM_DATA)
     public String uploadCV(
         @FormDataParam("file") InputStream uploadedInputStream,
         @FormDataParam("file") FormDataContentDisposition fileDetail) 
     {
         //saving the file object to the local disk
         saveToDisk(uploadedInputStream, fileDetail);
         return "File Uploaded Successfully";
     }//function uploadcv ends

     //Fuction to save uploaded file to disk
     private void saveToDisk(InputStream uploadedInputStream, 
         FormDataContentDisposition fileDetail) {
         String uploadedFileLocation = "D:\\University Study Stuff\\FYP\\Uploaded CVs For Project"+fileDetail.getFileName();
         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();

          }//try ends
          catch(IOException e)
          {e.printStackTrace();}
      }
  }
完成这些更改后,使用
mvn包创建war包,并部署/复制到
${TOMCAT\u HOME\webapps

检查样本资源是否正常工作

http://localhost:8080/{CONTEXT_PATH}/rest/cv

如果以上工作正常,请尝试上传资源的方法



另外,我没有测试你的上传代码,这是你的作业,你应该完成。

是web.xml中的包
com.tutorialpoint
,还是你自己的包root
com.quratulaind.us3
?我会尝试用com.quratulaind替换它。us3@Seánhaely我重新放置并再次导出n作为一场战争,但问题是constant@Qurat,您混合了
jersey 1
jersey 2
依赖项。@RishikeshDarandale您能详细说明我应该用什么替换什么吗?我只需要替换罐子吗?web.xml可以吗?包
com.tutorialspoint
应该放在web.xml还是shou中ld这是您自己的包root
com.quratulaind.us3
?我将尝试用com.quratulaind替换它。us3@Se我将其重新放置并再次作为战争输出,但问题是constant@Qurat,您混合了
jersey 1
jersey 2
依赖项。@RishikeshDarandale您能详细说明一下确切地说,我应该用什么替换?我只需要替换JAR吗?web.xml可以吗?我按照您的要求删除了所有库,但现在出现了一个“无法解决”的问题javax导入和org.glassfish导入出错我应该添加这些库吗?您是如何构建项目的?如果是maven,您应该使用命令
mvn clean package
来构建。如果使用eclipse,那么右键单击project。然后
maven->update project
应该为您提供
pom.xml中提到的所有引用库我按照您的要求删除了所有库,但现在出现了一个“无法解决”的问题javax导入和org.glassfish导入出错我应该添加这些库吗?您是如何构建项目的?如果是maven,您应该使用命令
mvn clean package
来构建。如果使用eclipse,那么右键单击project。然后
maven->update project
应该为您提供
pom.xml中提到的所有引用库
package com.quratulaind.us3;

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.Produces; 
import javax.ws.rs.core.MediaType;

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

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("cv")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String getIt() {
         return "Got it!";
     }

     @POST
     @Path("/upload")
     @Consumes(MediaType.MULTIPART_FORM_DATA)
     public String uploadCV(
         @FormDataParam("file") InputStream uploadedInputStream,
         @FormDataParam("file") FormDataContentDisposition fileDetail) 
     {
         //saving the file object to the local disk
         saveToDisk(uploadedInputStream, fileDetail);
         return "File Uploaded Successfully";
     }//function uploadcv ends

     //Fuction to save uploaded file to disk
     private void saveToDisk(InputStream uploadedInputStream, 
         FormDataContentDisposition fileDetail) {
         String uploadedFileLocation = "D:\\University Study Stuff\\FYP\\Uploaded CVs For Project"+fileDetail.getFileName();
         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();

          }//try ends
          catch(IOException e)
          {e.printStackTrace();}
      }
  }