2013-04-22 137 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

相关问题