[英]Can't deploy maven restful Upload file
package Restful.Demo.UploadFile;
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.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@Path("UploadFile")
public class Upload {
@GET
@Path("/upload1")
public String hello()
{
return "hello";
}
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)// use to upload file
public Response UploadFile(@FormDataParam("file") InputStream inputfile,
@FormDataParam("file") FormDataContentDisposition filemetadetail)
{
int statuscode=200;
String fileLocation="e://Restful/"+filemetadetail.getFileName();
writeFile(inputfile, fileLocation);
String output="File uploaded to "+fileLocation;
return Response.status(statuscode).entity(output).build();
}
/*
* @param inputFile the file you want to upload
* @param fileLocation where file will be uploaded
* @return 0 - success
* @throws IOException
*/
private void writeFile(InputStream inputFile,String fileLocation)
{
OutputStream output=null;
try
{
output=new FileOutputStream(new File(fileLocation));
int read=0;
byte[] bytes=new byte[1024];
//read
while((read=inputFile.read(bytes))!=-1)
output.write(bytes, 0, read);
output.flush();
output.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
你好。 我在Restful网络服务器中是彼得和新手。 上面是我使用Restful API的uploadfile代码-在Glassfish服务器上运行的Jersey在尝试将战争发布到glassfish服务器时出现此错误
java.lang.Exception:部署失败=名称为[UploadFile]的应用程序未部署在oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.pub上的oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.publishDeployedDirectory(GlassFishServerBehaviour.java:603)上(GlassFishServerBehaviour.java:830)在oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.publishModule(GlassFishServerBehaviour.java:790)在org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModule(ServerBehaviour:1 org.eclipse上的org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:987)上的org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModules(ServerBehaviourDelegate.java:1183)。 wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:774)位于org.eclipse.wst.server.core.internal.Server.publishImpl(Server.java:3182)位于org.eclipse.wst.server。 core.internal.Server $ PublishJob .org(Server.java:355)在org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)
谁能帮助我解决这个问题?
为了使您的资源类能够正常工作,您需要做一些事情。
Application
子类或web.xml
部署描述符。 9.3.1.2。 注册
在客户端/服务器代码中使用jersey-media-multipart模块的功能之前,需要注册MultiPartFeature。
也就是说,我在示例中添加了以下web.xml
,可以将应用程序部署在Glassfish 4上:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<!-- please see modified package name -->
<param-value>restful.demo.uploadfile</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-serlvet</servlet-name>
<!-- you can choose your favorite name here!! -->
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
笔记:
请参阅上面的注释部分,并尝试相应地修改您的代码。
您应该注意Java中的一些命名约定(有关更多详细信息,请参阅本文档):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.