2012-07-06 52 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/