2013-10-26 39 views
0

我正在开发一个有上传PDF文件功能的网页。但我有一个错误。春季MVC上传文件到服务器目录

这里是香港专业教育学院迄今所做的:

multipart解析器:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <!-- one of the properties available; the maximum file size in bytes --> 
    <property name="maxUploadSize" value="10000000"/> 
</bean> 

形式,将上传:

<form:form commandName="fileUpload" action="../admin/uploadPDF.do" method="post" enctype="multipart/form-data"> 
    <form:label path="fileData">Upload a File</form:label> <br /> 
    <form:input type="file" path="fileData" /> 
    <input type="submit" value="upload" > 
</form:form> 

控制器,赶上请求的用户第一次来上传AdminController.java

@RequestMapping(value = "/admin/module", method = RequestMethod.GET) 
    public String student(@RequestParam(defaultValue = "") 
    String message, @RequestParam(defaultValue = "") 
    String messageType, HttpServletRequest request, ModelMap model) 
    { 
     model.addAttribute("message", message); 
     model.addAttribute("messageType", messageType); 
     model.addAttribute(new UploadItemBean()); 
     HttpSession session = request.getSession(); 
     String returnVal = Credentials.checkSession(session); 

     if(returnVal != null) 
     { 
      return returnVal; 
     } 

     return "als-student/module"; 
    } 

控制器上传文件提交时,将捕获的要求,UploadController.java

@RequestMapping(value = "*/uploadPDF", method = RequestMethod.POST) 
public String getPDF(@RequestParam(defaultValue = "") 
String message, @RequestParam(defaultValue = "") 
String messageType, @RequestParam("name") 
String name, @RequestParam("file") 
MultipartFile file, HttpServletRequest request, ModelMap model) 
{ 
    ... 
    if(!file.isEmpty()) 
    { 
     try 
     { 
      byte[] bytes = file.getBytes(); 
      System.out.println(bytes + ", " + name); 
     } 
     catch(IOException e) 
     { 

      e.printStackTrace(); 
     } 
    } 
    return "als-student/module"; 
} 

堆栈跟踪:

Neither BindingResult nor plain target object for bean name 'fileUpload' available as request attribute 
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) 
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168) 
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188) 
    at org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130) 
    at org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120) 
    at org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90) 
... 
... 

我也想知道我怎么能豆fileUpload发送到形式,因为它似乎它是导致错误的原因之一。而且我在上传文件后,如何处理它以保存到apache服务器中的文件夹(如果这是最佳做法)?

回答

1

该片段

<form:form commandName="fileUpload" action="../admin/uploadPDF.do" method="post" enctype="multipart/form-data"> 
    <form:label path="fileData">Upload a File</form:label> <br /> 
    <form:input type="file" path="fileData" /> 
    <input type="submit" value="upload" > 
</form:form> 

由于commandName的期待一个模型(请求)属性与键fileUpload。你好像没有打消在Model这样的属性在你的处理器

@RequestMapping(value = "/admin/module", method = RequestMethod.GET) 
public String student(@RequestParam(defaultValue = "") 
String message, @RequestParam(defaultValue = "") 
String messageType, HttpServletRequest request, ModelMap model) 
{ 
    model.addAttribute("message", message); 
    model.addAttribute("messageType", messageType); 
    model.addAttribute(new UploadItemBean()); 
    HttpSession session = request.getSession(); 
    String returnVal = Credentials.checkSession(session); 

    if(returnVal != null) 
    { 
     return returnVal; 
    } 

    return "als-student/module"; 
} 

我假设你想要的UploadItemBean。只要改变你的代码要做到这一点

model.addAttribute("fileUpload", new UploadItemBean()); 

默认情况下,如果不指定属性的关键,Spring将基于对象的类名一个给你,这将不匹配预计fileUpload

+0

我明白了,那解决了我的第一个问题。我的第二个问题是如何将上传的文件保存在服务器的文件夹中?这不是一个好习惯吗? – newbie

+0

@newbie你如何保存取决于你。我建议你选择一个专用文件夹(如果它不存在,创建它)并直接在那里写你的文件。你可以用'MultipartFile#transferTo'方法做到这一点,或者直接读取'InputStream'或'byte []'并将其写入'OutputStream'。阅读'MultipartFile' javadoc。 –

+0

明白了,非常感谢 – newbie