我正在尝试在GlassFish Server 4.0上运行的REST项目中进行文件上传工作。
GlassFish服务器(虽然我发现它令人困惑)在javax.ws.rs库中有自己的Jersey库版本,到目前为止工作正常但是现在我需要在REST服务器上使用MediaType.MULTIPART_FORM_DATA和FormDataContentDisposition服务,但无法在GlassFish中找到它们。
因此我下载了Jersey库并添加了
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
到库,服务器端代码是
@ApplicationPath("webresources")
@Path("/file")
@Stateless
public class FileResource
{
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadWeb(@FormDataParam("file") InputStream inputStream,
@FormDataParam("file") FormDataContentDisposition disposition)
{
int read = 0;
byte[] bytes = new byte[1024];
try
{
while ((read = inputStream.read(bytes)) != -1)
{
System.out.write(bytes, 0, read);
}
}
catch (IOException ex)
{
Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex);
}
return Response.status(403).entity(inputStream).build();
}
}
现在无论什么时候调用REST资源(即使以前工作正常的资源),我都会收到错误:
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
如何修复上述错误?如何将lersey.multipart支持添加到GlassFish服务器?
答案 0 :(得分:1)
通过使用仅使用GlassFish可用库的以下服务器端代码,找到了解决方法:
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadWeb(InputStream inputStream)
{
DataInputStream dataInputStream = new DataInputStream(inputStream);
try
{
StringBuffer inputLine = new StringBuffer();
String tmp;
while ((tmp = dataInputStream.readLine()) != null)
{
inputLine.append(tmp);
System.out.println(tmp);
}
}
catch (IOException ex)
{
Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex);
}
return Response.status(403).entity(dataInputStream).build();
}