春季插入文件路径到MySQL [英] Spring inserting filepath to mysql

查看:73
本文介绍了春季插入文件路径到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Spring 3.2, Hibernate 4.1 and Mysql. I'm trying to save a file to the local drive and then save the filepath to the database to be used for future download. I have implemented the file upload to server but now I'm not sure who to go about saving the file path to the mysql table.

这是控制器代码

@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列中.任何想法都将受到欢迎.

The filepath is to be inserted into the document_path column. Any thoughts would be welcome.

推荐答案

首先,module_id由数据库生成,因此您不应该从前端接受它.如果您希望数据库中的下一个条目为30,而我将module_id发送为24,该怎么办?

First, the module_id is generated by the database, so you shouldn't be accepting it from the front end. What if I send module_id as 24 when you expect the next entry in the DB is 30?

现在输入代码:

@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";
}

这篇关于春季插入文件路径到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆