我现在正在学习使用 Java 创建 Web 服务。准确地说,是带有 JAX-RS 的 RESTful Webservices 及其实现 Jersey。我正在 Eclipse 中开发一个动态 Web 项目。
Web 服务应允许用户从他的计算机上载文件(这可以工作),然后保存在服务器上(也可以工作)。上传/保存后,服务应返回指定文件的 URI,并可用于与 Internet 上的其他人共享文件(这不起作用)。
泽西框架可以做到这一点吗?
我试图对这个主题进行一些研究,但由于我对这个主题相当陌生,所以我似乎在搜索错误的关键字。
所以这就是我想出的:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
@Path("/file")
public class Upload {
@Context
private UriInfo uriInfo;
@Context
private ServletContext context;
@GET
@Path("/upload")
@Produces(MediaType.TEXT_HTML)
public String getUploadForm() {
StringBuilder htmlPageBuilder = new StringBuilder();
htmlPageBuilder.append("<html>");
htmlPageBuilder.append("<body>");
htmlPageBuilder.append("<h1>File upload</h1>");
htmlPageBuilder.append("<form action=\"rest/file/processed\" method=\"post\" enctype=\"multipart/form-data\">");
htmlPageBuilder.append("<p>");
htmlPageBuilder.append("Select a file : <input type=\"file\" name=\"file\" size=\"45\" />");
htmlPageBuilder.append("</p>");
htmlPageBuilder.append("<input type=\"submit\" value=\"Upload file\" />");
htmlPageBuilder.append("</form>");
htmlPageBuilder.append("</body>");
htmlPageBuilder.append("</html>");
return htmlPageBuilder.toString();
}
@POST
@Path("/rest/file/processed")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream uploadInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFilePath = "C:\\Tomcat\\Data\\";
uploadedFilePath += fileDetail.getFileName();
//write the file onto the server
File uploadedFile = this.writeToFile(uploadInputStream, uploadedFilePath);
String uploadedFileName = uploadedFile.getName();
String output = "File uploaded to: " + uploadedFilePath + uploadedFileName;
URI location = uriInfo.getAbsolutePathBuilder().path(uploadedFileName).build();
//return Response.ok(output).build();
return Response.created(location).entity(Entity.text(output)).build();
}
private File writeToFile(InputStream uploadInputStream, String uploadedFilePath) {
File uploadedFile = new File(uploadedFilePath);
try {
OutputStream out = new FileOutputStream(uploadedFile);
int read = 0;
byte[] bytes = new byte[1024];
while((read = uploadInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
}
}
}
所以对于 POST 方法返回 Response 为 OK。作为 URI 的另一个响应甚至没有出现(我注释掉的部分)。我认为原因是资源不存在。
我真的很感激任何帮助。
编辑
我尝试了 Aradom 提供的解决方案并包含了 genson 库。现在我得到了一个新的NullPointerException
. 这是堆栈跟踪:
Severe: Servlet.service() for servlet [RestServlet] in context with path [/FirstRestWebService] threw exception
java.lang.NullPointerException
at com.owlike.genson.reflect.BeanViewDescriptorProvider$BeanViewPropertyFactory.createAccessor(BeanViewDescriptorProvider.java:122)
at com.owlike.genson.reflect.BeanPropertyFactory$CompositeFactory.createAccessor(BeanPropertyFactory.java:49)
at com.owlike.genson.reflect.BaseBeanDescriptorProvider.provideMethodAccessors(BaseBeanDescriptorProvider.java:187)
at com.owlike.genson.reflect.BaseBeanDescriptorProvider.provideBeanPropertyAccessors(BaseBeanDescriptorProvider.java:94)
at com.owlike.genson.reflect.AbstractBeanDescriptorProvider.provide(AbstractBeanDescriptorProvider.java:106)
at com.owlike.genson.convert.BasicConvertersFactory.provide(BasicConvertersFactory.java:109)
at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:74)
at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:56)
at com.owlike.genson.reflect.AbstractBeanDescriptorProvider$ContextualFactoryDecorator.create(AbstractBeanDescriptorProvider.java:79)
at com.owlike.genson.reflect.AbstractBeanDescriptorProvider$ContextualFactoryDecorator.create(AbstractBeanDescriptorProvider.java:68)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:58)
at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:22)
at com.owlike.genson.Genson.provideConverter(Genson.java:196)
at com.owlike.genson.Genson.serialize(Genson.java:340)
at com.owlike.genson.ext.jaxrs.GensonJsonConverter.writeTo(GensonJsonConverter.java:91)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我尝试查看 genson 库,看看是否能找到有关此异常的信息。到目前为止,我非常感谢您的帮助。