2012-07-06 53 views
3

我正嘗試使用REST服務設置使用html和Spring 3.0.6的簡單上載。我在線學習了該教程,但MultipartFile參數始終爲空。這裏的配置和代碼:Spring REST MultipartFile文件始終爲空

應用程序的context.xml:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="maxUploadSize" value="2000000"/> 
</bean> 

的pom.xml:

<dependency> 
    <groupId>commons-io</groupId> 
    <artifactId>commons-io</artifactId> 
    <version>2.1</version> 
</dependency> 
<dependency> 
    <groupId>commons-fileupload</groupId> 
    <artifactId>commons-fileupload</artifactId> 
    <version>1.2.2</version> 
</dependency> 

HTML:

<html> 
    <head> 
     <title>Upload a file please</title> 
    </head> 
    <body> 
     <h1>Please upload a file</h1> 
     <form method="post" action="/site/restServices/artworkUpload/" enctype="multipart/form-data"> 
      <input type="text" name="name"/> 
      <input type="file" name="file"/> 
      <input type="submit"/> 
     </form> 
    </body> 
</html> 

REST控制器:

@POST 
@Path("/artworkUpload") 
public String uploadFile(@RequestParam("name") String name, 
    @RequestParam("file") MultipartFile file) { 
    try { 
     if (!file.isEmpty()) { 
      byte[] bytes = file.getBytes(); 
      // store the bytes somewhere 
      return "redirect:uploadSuccess"; 
     } else { 
      return "redirect:uploadFailure"; 
     } 
    } 
    catch (Exception ex) 
    { 

    } 
    return null; 
} 

我複製了Spring的教程中的例子,但不管我改變了什麼,文件參數都是空的。 「名稱」將在文本框中具有值,但文件將爲空。

我也嘗試使用澤西和我收到該文件的InputStream但FormDataContentDisposition爲空,所以我無法確定文件類型。

這也在Jetty上運行。

我錯過了什麼?

回答

3

我記得我解決了同樣的問題,通過把額外的庫到我的構建路徑:

commons-fileupload-1.2.2.jar 
commons-io-2.1.jar 

我希望這會幫助你。

編輯。

好的。最後我有時間處理這個問題。首先,爲什麼你使用standart java特性來構建rest服務(annotations @POST,@Path)?因爲對於Spring來說,使用Spring MVC未來版本更適合於REST。在互聯網上有很多這方面的信息。這裏是reference documentation的特殊部分。這裏也是good article on IBM site。關於如何使用Spring MVC構建REST控制器的很好的描述在Spring in Action (last 3-d edition)中。

這裏如何我已經實現簡單的文件上傳功能:

休息控制器:

@Controller 
@RequestMapping("/rest/files") 
public class FilesController { 
     ... 

     @RequestMapping(value="/rest/files", method=RequestMethod.POST) 
     public String uploadFile(@RequestParam("name") String name, 
       @RequestParam("file") MultipartFile file) { 
      try { 
       if (!file.isEmpty()) { 
        byte[] bytes = file.getBytes(); 
        // store the bytes somewhere 
        return "redirect:uploadSuccess"; 
       } else { 
        return "redirect:uploadFailure"; 
       } 
      } 
      catch (Exception ex) 
      { 

      } 
      return "/testFileDownload"; 
     } 
} 

HTML:dispatcher-servlet.xml文件

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Test file upload</title> 
</head> 
<body> 
    <h1>Please upload a file</h1> 
    <form method="post" action="rest/files" enctype="multipart/form-data"> 
     <input type="text" name="name" /> <input type="file" name="file" /> <input 
      type="submit" /> 
    </form> 
</body> 
</html> 

查看解析器配置:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
     <property name="mediaTypes"> 
      <map> 
       <entry key="file" value="multipart/form-data"/> 
       <entry key="html" value="text/html"/> 
      </map> 
     </property> 
     <property name="viewResolvers"> 
      <list> 
       <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> 
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/views/"/> 
        <property name="suffix" value=".jsp"/> 
       </bean> 
      </list> 
     </property> 
    </bean> 

我希望我不會浪費我的時間,這對你仍然是必要的。 )

EDIT 2

這裏是very good tutorial其中描述瞭如何使用Spring 3.1構建RESTful Web服務。

+0

我有這兩個在我的Maven構建路徑(pom.xml中)。我已檢查並將它們複製到lib文件夾。 – 2012-07-06 17:26:04

+0

我更新了我的答案。看看這些變化。 – dimas 2012-07-08 20:11:20

+0

我仍然需要它。謝謝!我正在嘗試你的解決方案。 – 2012-07-12 00:55:54

1

它幫助我連接這個庫:

<dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 

所有庫:

<dependencies> 
    <!-- Spring 3 MVC --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>3.1.2.RELEASE</version> 
    </dependency> 
    <!-- Apache Commons file upload --> 
    <dependency> 
     <groupId>commons-fileupload</groupId> 
     <artifactId>commons-fileupload</artifactId> 
     <version>1.2.2</version> 
    </dependency> 
    <!-- Apache Commons IO --> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-io</artifactId> 
     <version>1.3.2</version> 
    </dependency> 
    <!-- JSTL for c: tag --> 
    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
</dependencies> 

http://viralpatel.net/blogs/spring-mvc-multiple-file-upload-example/