2013-04-22 136 views
0

我使用彈簧框架3.2爲我的項目,我有一個窗體與許多表單元素和上傳功能。我想將表單保存到數據庫中,同時將文件上傳到本地驅動器,然後將文件的路徑保存到數據庫。春天上傳到服務器並保存到數據庫的文件路徑

稍後將使用文件路徑來檢索文件。

我能夠將表單保存爲一個對象本身到數據庫,但我不知道如何去集成上傳保存到服務器並將路徑添加到數據庫。我將不勝感激任何關於如何去做的指示。

回答

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



@Controller 
public class FileUploadController { 

@RequestMapping(value = "/form", method = RequestMethod.POST) 
public String handleFormUpload(@RequestParam("name") String name, 
    @RequestParam("file") MultipartFile file) { 

    if (!file.isEmpty()) { 
     byte[] bytes = file.getBytes(); 
     // store the bytes somewhere 
     return "redirect:uploadSuccess"; 
    } else { 
     return "redirect:uploadFailure"; 
    } 
} 

} 

reference 1

step by step

相關問題