找不到资源 Jersey 2.x
Resource not found Jersey 2.x
我正在尝试创建一个网络服务来将文件上传到服务器。我无法访问该服务并且总是收到 404 错误,即找不到资源。我无法弄清楚我的代码有什么问题。请帮忙。
下面是我的web.xml。我还注册了其他资源,如登录、注册和曲目,这些资源通过 ajax 请求工作正常,但上传器网络服务不工作。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>mixtri</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<display-name>mixtri</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.mixtri.tracks,com.mixtri.login,com.mixtri.signup,com.mixtri.uploader</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 REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
下面是我的 html 表单的标记:
<form method="post" action="/mixtri/rest/upload"
enctype="multipart/form-data">
<table align="center" border="1" bordercolor="black" cellpadding="0"
cellspacing="0">
<tr>
<td>Select Zip File :</td>
<td><input type="file" name="uploadFile" size="100" /></td>
</tr>
<tr>
<td><input type="submit" value="Upload File" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
这是我的资源 Class:
package com.mixtri.uploader;
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 javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@Path("/rest")
public class FileServiceImpl implements IFileService {
public static final String UPLOAD_FILE_SERVER = "D:/Demo/upload/";
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadZippedFile(
@FormDataParam("uploadFile") InputStream fileInputStream,
@FormDataParam("uploadFile") FormDataContentDisposition fileFormDataContentDisposition) {
// local variables
String fileName = null;
String uploadFilePath = null;
try {
fileName = fileFormDataContentDisposition.getFileName();
uploadFilePath = writeToFileServer(fileInputStream, fileName);
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
// release resources, if any
}
return Response.ok("File uploaded successfully at " + uploadFilePath).build();
}
/**
* write input stream to file server
* @param inputStream
* @param fileName
* @throws IOException
*/
private String writeToFileServer(InputStream inputStream, String fileName) throws IOException {
OutputStream outputStream = null;
String qualifiedUploadFilePath = UPLOAD_FILE_SERVER + fileName;
try {
outputStream = new FileOutputStream(new File(qualifiedUploadFilePath));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.flush();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
finally{
//release resource, if any
outputStream.close();
}
return qualifiedUploadFilePath;
}
}
任何指点都会有所帮助。
您可以将 FileServiceImpl
中的 @Path("/rest")
更改为 @Path("/")
。或者您可以将其更改为 @Path("/upload")
并将其从 uploadZippedFile
方法中删除。
您已将 /rest
定义为此处 Jersey 应用程序的根映射
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
所以最终你的路径需要是
action="/mixtri/rest/rest/upload"
如果您不更改注释。
我正在尝试创建一个网络服务来将文件上传到服务器。我无法访问该服务并且总是收到 404 错误,即找不到资源。我无法弄清楚我的代码有什么问题。请帮忙。
下面是我的web.xml。我还注册了其他资源,如登录、注册和曲目,这些资源通过 ajax 请求工作正常,但上传器网络服务不工作。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>mixtri</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<display-name>mixtri</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.mixtri.tracks,com.mixtri.login,com.mixtri.signup,com.mixtri.uploader</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 REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
下面是我的 html 表单的标记:
<form method="post" action="/mixtri/rest/upload"
enctype="multipart/form-data">
<table align="center" border="1" bordercolor="black" cellpadding="0"
cellspacing="0">
<tr>
<td>Select Zip File :</td>
<td><input type="file" name="uploadFile" size="100" /></td>
</tr>
<tr>
<td><input type="submit" value="Upload File" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
这是我的资源 Class:
package com.mixtri.uploader;
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 javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@Path("/rest")
public class FileServiceImpl implements IFileService {
public static final String UPLOAD_FILE_SERVER = "D:/Demo/upload/";
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadZippedFile(
@FormDataParam("uploadFile") InputStream fileInputStream,
@FormDataParam("uploadFile") FormDataContentDisposition fileFormDataContentDisposition) {
// local variables
String fileName = null;
String uploadFilePath = null;
try {
fileName = fileFormDataContentDisposition.getFileName();
uploadFilePath = writeToFileServer(fileInputStream, fileName);
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
// release resources, if any
}
return Response.ok("File uploaded successfully at " + uploadFilePath).build();
}
/**
* write input stream to file server
* @param inputStream
* @param fileName
* @throws IOException
*/
private String writeToFileServer(InputStream inputStream, String fileName) throws IOException {
OutputStream outputStream = null;
String qualifiedUploadFilePath = UPLOAD_FILE_SERVER + fileName;
try {
outputStream = new FileOutputStream(new File(qualifiedUploadFilePath));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.flush();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
finally{
//release resource, if any
outputStream.close();
}
return qualifiedUploadFilePath;
}
}
任何指点都会有所帮助。
您可以将 FileServiceImpl
中的 @Path("/rest")
更改为 @Path("/")
。或者您可以将其更改为 @Path("/upload")
并将其从 uploadZippedFile
方法中删除。
您已将 /rest
定义为此处 Jersey 应用程序的根映射
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
所以最终你的路径需要是
action="/mixtri/rest/rest/upload"
如果您不更改注释。