【问题标题】:Spring inserting filepath to mysqlSpring将文件路径插入mysql
【发布时间】:2013-05-20 21:08:40
【问题描述】:

我正在使用 Spring 3.2、Hibernate 4.1 和 Mysql。我正在尝试将文件保存到本地驱动器,然后将文件路径保存到数据库以供将来下载。我已经实现了文件上传到服务器,但现在我不确定谁去保存文件路径到 mysql 表。

这是控制器代码

@RequestMapping(value = "/add", method = RequestMethod.POST, params = "save")
public String saveProcess(@RequestParam("moduleId") Integer moduleId, 

@ModelAttribute("module") Module module, BindingResult result, 
@RequestParam("file") CommonsMultipartFile[] file ,
HttpSession session, HttpServletRequest request) throws   

 IllegalStateException, IOException {
    logger.info("Request to save the module");

    if(module != null){

    if (file != null && file.length > 0) {
         for (CommonsMultipartFile aFile : file){

             System.out.println("Saving file: " + aFile.getOriginalFilename());

             if (!aFile.getOriginalFilename().equals("")) {
                 aFile.transferTo(new File(saveDirectory +    

             aFile.getOriginalFilename()));
             }
         }
     }


     moduleService.saveorupdate(module);

}
    return "redirect:/home";
}

这是数据库

 CREATE TABLE `modules` (
`module_id` bigint(20) NOT NULL AUTO_INCREMENT,
`document_title` varchar(255) DEFAULT NULL,
`document_path` varchar(255) DEFAULT NULL,
PRIMARY KEY (`module_id`);

文件路径将被插入到 document_path 列中。欢迎任何想法。

【问题讨论】:

  • 有什么问题?你被困在哪里了?
  • 感谢您的回复。我想知道现在如何保存我上传到数据库表的文件的文件路径。

标签: mysql spring hibernate


【解决方案1】:

首先,module_id 是由数据库生成的,所以你不应该从前端接受它。如果当您期望 DB 中的下一个条目是 30 时我将 module_id 发送为 24 会怎样?

现在是代码:

@RequestMapping(value = "/add", method = RequestMethod.POST, params = "save")
public String saveProcess(@RequestParam("moduleId") Integer moduleId, @ModelAttribute("module") Module module, BindingResult result, @RequestParam("file") CommonsMultipartFile[] file) throws IllegalStateException, IOException {
    logger.info("Request to save the module");

    if(module != null) { // No need for the check. Spring guarantees a non null object if it is expected to be a @ModelAttribute. 

    if (file != null && file.length > 0) {
         for (CommonsMultipartFile aFile : file){ 
             Module module = new Module(); //only one file or more? If one then use the @ModelAttribute module.

             if (!"".equals(aFile.getOriginalFilename())) { // Best practice to check equals in reverse to avoid potential NullPointerExceptions.
                 File file = new File(saveDirectory + aFile.getOriginalFilename());
                 aFile.transferTo(file);
                 module.setDocumentTitle(aFile.getOriginalFilename());
                 module.setDocumentPath(file.getAbsolutePath());
                 module = moduleService.saveorupdate(module); // return module or id so that you can set it and pass it to the view.
             }
         }
     }
  }
  return "redirect:/home";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 2011-05-17
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多