我使用restwebserivce做了一个上传文件的示例应用程序。 这不是Maven project.only示例项目。 这是web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<display-name>JAX RS Application</display-name>
<servlet>
<servlet-name>REST Service</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.eb.rajesign.util</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>REST Service</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
这是HTML代码:
<body>
<form action="resource/restwb/upload" method="post" enctype="multipart/form-data">
File : <input type="file" name="upload" size="50" />
<br/>
<input type="submit" value="Upload" />
</form>
和java文件:
package com.eb.rajesign.util;
@Path("/restwb")
public class FileResource {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("upload") InputStream is, @FormDataParam("upload") FormDataContentDisposition formData) {
System.out.println("I am in service");
String fileLocation = "D:/TestRaj/" + formData.getFileName();
try {
saveFile(is, fileLocation);
String result = "Successfully File Uploaded on the path " + fileLocation;
return Response.status(Status.OK).entity(result).build();
} catch (IOException e) {
e.printStackTrace();
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
}
private void saveFile(InputStream is, String fileLocation) throws IOException {
OutputStream os = new FileOutputStream(new File(fileLocation));
byte[] buffer = new byte[256];
int bytes = 0;
while ((bytes = is.read(buffer)) != -1) {
os.write(buffer, 0, bytes);
}
}}
我添加了jar文件列表:
球衣束-1.6.jar 新泽西州的核心1.18.jar 新泽西州的多,1.18.1.jar 新泽西服务器1.18.1.jar 新泽西州的servlet-1.18.1.jar mimepull-1.9.3.jar 球衣束,1.14.jar ASM-3.3.1.jar
我无法上传文件。你能建议我吗? 得到错误: [11/18/15 22:18:40:147 IST] 00000215 ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper init Uncaught.init.exception.thrown.by.servlet [11/18/15 22:18:40:147 IST] 00000215 webapp E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E:[Servlet Error] - [REST Service]:java.lang.IllegalAccessError :class com / sun / jersey / spi / scanning / AnnotationScannerListener非法访问com / sun / jersey / core / reflection / ReflectionHelper类的“私有”成员 在com.sun.jersey.spi.scanning.AnnotationScannerListener。(AnnotationScannerListener.java:92)