【发布时间】:2017-10-28 11:57:44
【问题描述】:
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 上传文件的代码 - Jersey 在 Glassfish 服务器上运行 我在尝试将战争发布到 glassfish 服务器时遇到此错误
java.lang.Exception: deploy is failed=Application with name [UploadFile] 未部署在 oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.publishDeployedDirectory(GlassFishServerBehaviour.java:603) 在 oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.publishModuleForGlassFishV3(GlassFishServerBehaviour.java:830) 在 oracle.eclipse.tools.glassfish.GlassFishServerBehaviour.publishModule(GlassFishServerBehaviour.java:790) 在 org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModule(ServerBehaviourDelegate.java:1091) 在 org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModules(ServerBehaviourDelegate.java:1183) 在 org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:987) 在 org.eclipse.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.run(Server.java:355) 在 org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)
谁能帮我解决这个问题?
【问题讨论】:
-
您提供的信息太少,无法为您提供帮助。发布
web.xml,什么版本的 Jersey、Servlet、Glassfish,...,你正在运行。 -
我没有使用 servlet 。只是一个带有 post 表单的 HTML 文件。
-
glassfish 4.1.1 。球衣 2.26
-
编辑问题并发布
web.xml的相关内容。 -
对不起。我的项目中没有 web.xml。只有 pom.xml
标签: java rest maven jersey glassfish