jersey上传文件解决办法

         这两天在使用jersey  构建的jersey JAX-RS REST服务器,在通过POST方法上传文件的时候,如果根据example来操作的话会引发如下异常:

1 SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.lotame.ws.api.resources.FileResource.uploadFile(java.io.InputStream,com.   sun.jersey.core.header.FormDataContentDisposition) at parameter at index 0

2 SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.lotame.ws.api.resources.FileResource.uploadFile(java.io.InputStream,com.   sun.jersey.core.header.FormDataContentDisposition) at parameter at index 1

3 SEVERE: Method, public javax.ws.rs.core.Response com.lotame.ws.api.resources.FileResource.uploadFile(java.io.InputStream,com.sun.jersey.core.header  .FormDataContentDisposition), annotated with POST of resource, class com.lotame.ws.api.resources.FileResource, is not recognized as valid resource   method.

        或者

1  HTTP 415-Unsupported Media Type Error

      在网上查了好多,貌似都不能解决我遇到的问题,然后就不用他本身提供的例子,下面是我实现上传功能的例子:

 1:创建一个JAVA Web工程:

 jersey上传文件解决办法

2:XML文件

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

 3   <display-name>Restful Web Application</display-name>

 4 

 5     <servlet>

 6         <servlet-name>jersey-helloworld-serlvet</servlet-name>

 7         <servlet-class>

 8                      com.sun.jersey.spi.container.servlet.ServletContainer

 9                 </servlet-class>

10         <init-param>

11              <param-name>com.sun.jersey.config.property.packages</param-name>

12              <param-value>upload</param-value>

13         </init-param>

14         <load-on-startup>1</load-on-startup>

15     </servlet>

16 

17     <servlet-mapping>

18         <servlet-name>jersey-helloworld-serlvet</servlet-name>

19         <url-pattern>/rest/*</url-pattern>

20     </servlet-mapping>

21 </web-app>

3:ImageResource类中的内容

 1 package upload;

 2 

 3 import java.io.File;

 4 import java.util.Iterator;

 5 import java.util.List;

 6 

 7 import javax.servlet.http.HttpServletRequest;

 8 import javax.ws.rs.Consumes;

 9 import javax.ws.rs.POST;

10 import javax.ws.rs.Path;

11 import javax.ws.rs.Produces;

12 import javax.ws.rs.core.Context;

13 import javax.ws.rs.core.MediaType;

14 

15 import org.apache.tomcat.util.http.fileupload.FileItem;

16 import org.apache.tomcat.util.http.fileupload.FileItemFactory;

17 import org.apache.tomcat.util.http.fileupload.FileUploadException;

18 import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;

19 import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

20 

21 @Path("/images")

22 @Consumes(MediaType.MULTIPART_FORM_DATA)

23 public class ImageResource {

24     private static final String SERVER_UPLOAD_LOCATION_FOLDER = "C://";

25 

26     @POST

27     @Produces(MediaType.APPLICATION_JSON)

28     public String uploadStatePolicy(@Context HttpServletRequest request) {

29         try {

30             String fileName = saveFile(request);

31             if (!fileName.equals("")) {

32             } else {

33 

34             }

35         } catch (Exception ex) {

36         }

37         return "";

38     }

39 

40     private String saveFile(HttpServletRequest request) {

41         String fileName = "";

42         try {

43             if (ServletFileUpload.isMultipartContent(request)) {

44                 FileItemFactory factory = new DiskFileItemFactory();

45                 ServletFileUpload upload = new ServletFileUpload(factory);

46                 List<FileItem> items = null;

47                 try {

48                     items = upload.parseRequest(request);

49                 } catch (FileUploadException e) {

50                     e.printStackTrace();

51                 }

52                 if (items != null) {

53                     Iterator<FileItem> iter = items.iterator();

54                     while (iter.hasNext()) {

55                         FileItem item = iter.next();

56                         if (!item.isFormField() && item.getSize() > 0) {

57                             fileName = processFileName(item.getName());

58                             try {

59                                 item.write(new File(

60                                         SERVER_UPLOAD_LOCATION_FOLDER

61                                                 + fileName));

62                             } catch (Exception e) {

63                                 e.printStackTrace();

64                             }

65                         }

66                     }

67                 }

68             }

69         } catch (Exception e) {

70         }

71         return fileName;

72     }

73 

74     private String processFileName(String fileNameInput) {

75         String fileNameOutput = null;

76         fileNameOutput = fileNameInput.substring(

77                 fileNameInput.lastIndexOf("\\") + 1, fileNameInput.length());

78         return fileNameOutput;

79     }

80 }

4:form.html文件

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">

 2 <html>

 3 <head>

 4 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

 5 <title>Form Page</title>

 6 </head>

 7 <body>

 8 <h1>Upload a File</h1>

 9 

10     <form action="http://localhost:8080/JerseyUpLoad/rest/images" method="post" enctype="multipart/form-data">

11 

12        <p>

13         Select a file : <input type="file" name="file" size="50" />

14        </p>

15 

16        <input type="submit" value="Upload It" />

17     </form>

18 

19 </body>

20 </html>

5运行即可。

 

你可能感兴趣的:(jersey)