2013-10-26 68 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