我想使用 restfull webservice上传文件。当我曝光时 这个休息服务,我得到500 *(内部服务器错误)*。
resource.java类
@Path("/resource")
public class Demo{
private static final String FOLDER_PATH = "C:/uploaded_license/";
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String uploadFile(@FormDataParam("file") InputStream fis,
@FormDataParam("file") FormDataContentDisposition fdcd) {
OutputStream outpuStream = null;
String fileName = fdcd.getFileName();
System.out.println("File Name: " + fdcd.getFileName());
String filePath = FOLDER_PATH + fileName;
try {
int read = 0;
byte[] bytes = new byte[1024];
outpuStream = new FileOutputStream(new File(filePath));
while ((read = fis.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
} catch(IOException iox){
iox.printStackTrace();
} finally {
if(outpuStream != null){
try{outpuStream.close();} catch(Exception ex){}
}
}
return "File Upload Successfully !!";
}
}
This is the rest service in which *FOLDER_PATH* is the path where the
uploaded file will saved.
upload.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery jsTree Plugin Demo</title>
</head>
<form action="rest/resource/upload" method="post"
enctype="multipart/form-data">
<p>Select a file : <input type="file" name="file"/></p>
<input type="submit" value="Upload File" />
</form>
</div>
</body>
</html>
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<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>uploadfile</display-name>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>mypackage.upload</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
的pom.xml
定义所有依赖项的pom.xml -
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
错误
HTTP Status 500 - Servlet.init() for servlet jersey-servlet threw exception
type Exception report
message Servlet.init() for servlet jersey-servlet threw exception
description The server encountered an internal error that prevented it from
fulfilling this request.
exception
javax.servlet.ServletException: Servlet.init() for servlet jersey-servlet
threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke
(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke
(AbstractAccessLogValve.java:620)
org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:502)
org.apache.coyote.http11.AbstractHttp11Processor.process
(AbstractHttp11Processor.java:1132)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process
(AbstractProtocol.java:684)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun
(NioEndpoint.java:1533)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run
(NioEndpoint.java:1489)java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run
(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
root cause
org.glassfish.jersey.server.model.ModelValidationException: Validation of
the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public
java.lang.String
mypackage.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.
FormDataContentDisposition) at index 0.;
source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-
data], producedTypes=[text/plain], suspended=false, suspendTimeout=0,
suspendTimeoutUnit=MILLISECONDS,
invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class
mypackage.upload, handlerConstructors=
[org.glassfish.jersey.server.model.HandlerConstructor@39ede858]},
definitionMethod=public java.lang.String
mypackage.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.
FormDataContentDisposition), parameters=[Parameter [type=class
java.io.InputStream, source=file, defaultValue=null], Parameter [type=class
org.glassfish.jersey.media.multipart.FormDataContentDisposition,
source=file, defaultValue=null]], responseType=class java.lang.String},
nameBindings=[]}']
org.glassfish.jersey.server.ApplicationHandler.initialize
(ApplicationHandler.java:555)
org.glassfish.jersey.server.ApplicationHandler.access$500
(ApplicationHandler.java:184)
org.glassfish.jersey.server.ApplicationHandler$3.call
(ApplicationHandler.java:350)
org.glassfish.jersey.server.ApplicationHandler$3.call
(ApplicationHandler.java:347)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.processWithException
(Errors.java:255)
org.glassfish.jersey.server.ApplicationHandler.<init>
(ApplicationHandler.java:347)
org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
org.glassfish.jersey.servlet.ServletContainer.init
(ServletContainer.java:177)
org.glassfish.jersey.servlet.ServletContainer.init
(ServletContainer.java:369)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke
(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke
(AbstractAccessLogValve.java:620)
org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:502)
org.apache.coyote.http11.AbstractHttp11Processor.process
(AbstractHttp11Processor.java:1132)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process
(AbstractProtocol.java:684)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun
(NioEndpoint.java:1533)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run
(NioEndpoint.java:1489)
java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run
(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
我认为你需要inti ApplicationContext.xml
。防爆。 : -
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>