这两天在使用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上传文件解决办法_第1张图片

2:XML文件

 1 "1.0" encoding="UTF-8"?>   2 "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   Restful Web Application   4    5        6         jersey-helloworld-serlvet   7         class>   8                      com.sun.jersey.spi.container.servlet.ServletContainer   9                 class>  10           11              com.sun.jersey.config.property.packages  12              upload  13           14         1  15       16   17       18         jersey-helloworld-serlvet  19         /rest/*  20       21 

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 items = null;  47                 try {  48                     items = upload.parseRequest(request);  49                 } catch (FileUploadException e) {  50                     e.printStackTrace();  51                 }  52                 if (items != null) {  53                     Iterator 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 "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">   2    3    4 "Content-Type" content="text/html; charset=ISO-8859-1">   5 Form Page   6    7    8 

Upload a File

9 10
"http://localhost:8080/JerseyUpLoad/rest/images" method="post" enctype="multipart/form-data"> 11 12

13 Select a file : "file" name="file" size="50" /> 14

15 16 "submit" value="Upload It" /> 17
18 19 20

5运行即可。