如何将文件上传到 java 中的 restful 网络服务?
How do you upload a file to a restful web service in java?
我对在 java 中构建 REST Web 服务相当陌生(在 .NET 世界中构建了多个),我 运行 遇到了一个问题,我的 google-fu 是也没有给出答案。我有一个 post 方法:
@POST
@Consumes(value = MediaType.MULTIPART_FORM_DATA)
public Response postFile(@FormDataParam("file") InputStream content,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String fileName = fileDetail.getName();
String ext = getFileExtension(fileName);
URI uri = context.getBaseUri();
UUID userId = processData.SubmitData(content, ext);
return Response.created(URI.create(uri + "/api/appuser/" + userId)).build();
}
当我尝试 post 时返回 404。我使用的是最新版本的 Jax-rs、glass fish 等。
我的web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ScoringApp</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>
<servlet>
<servlet-name>Scoring Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Enable Tracing support. -->
<init-param>
<param-name>jersey.config.server.tracing</param-name>
<param-value>ALL</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.scoringapp.api</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.scoringapp.api.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Scoring Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
我的申请 class:
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register resources and features
classes.add(MultiPartFeature.class);
classes.add(FileResource.class);
classes.add(LoggingFilter.class);
classes.add(CORSResponseFilter.class);
classes.add(CORSRequestFilter.class);
return classes;
}
}
任何其他帮助我解决这个问题的方法都很好。
(运行 这在 Maven 中,使用动态 Web 应用程序,我遵循了教程 :) 还有几个其他测试端点工作正常,所以我知道我可以访问它并达到基础 url returns 我创建的他 index.html 只是为了确保一切都在那里。)
看来我的问题没有正确遵循 HTML 规范并没有注意我的 URI 大小写。我的坏...
我对在 java 中构建 REST Web 服务相当陌生(在 .NET 世界中构建了多个),我 运行 遇到了一个问题,我的 google-fu 是也没有给出答案。我有一个 post 方法:
@POST
@Consumes(value = MediaType.MULTIPART_FORM_DATA)
public Response postFile(@FormDataParam("file") InputStream content,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String fileName = fileDetail.getName();
String ext = getFileExtension(fileName);
URI uri = context.getBaseUri();
UUID userId = processData.SubmitData(content, ext);
return Response.created(URI.create(uri + "/api/appuser/" + userId)).build();
}
当我尝试 post 时返回 404。我使用的是最新版本的 Jax-rs、glass fish 等。
我的web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ScoringApp</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>
<servlet>
<servlet-name>Scoring Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Enable Tracing support. -->
<init-param>
<param-name>jersey.config.server.tracing</param-name>
<param-value>ALL</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.scoringapp.api</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.scoringapp.api.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Scoring Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
我的申请 class:
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register resources and features
classes.add(MultiPartFeature.class);
classes.add(FileResource.class);
classes.add(LoggingFilter.class);
classes.add(CORSResponseFilter.class);
classes.add(CORSRequestFilter.class);
return classes;
}
}
任何其他帮助我解决这个问题的方法都很好。 (运行 这在 Maven 中,使用动态 Web 应用程序,我遵循了教程 :) 还有几个其他测试端点工作正常,所以我知道我可以访问它并达到基础 url returns 我创建的他 index.html 只是为了确保一切都在那里。)
看来我的问题没有正确遵循 HTML 规范并没有注意我的 URI 大小写。我的坏...