【问题标题】:Spring Upload to server and saving file path to databaseSpring上传到服务器并将文件路径保存到数据库
【发布时间】:2013-04-22 23:52:31
【问题描述】:

我在我的项目中使用 spring 框架 3.2,我有一个包含许多表单元素和上传功能的表单。我想将表单保存到数据库,同时将文件上传到我的本地驱动器,然后将文件的路径保存到数据库。

文件路径将用于稍后检索文件。

我可以将表单作为对象单独保存到数据库中,但我不确定如何将上传保存到服务器并添加到数据库的路径。我将不胜感激有关如何去做的任何指示。

【问题讨论】:

    标签: spring file download


    【解决方案1】:
    <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

    【讨论】:

      猜你喜欢
      • 2020-01-28
      • 2014-03-18
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      相关资源
      最近更新 更多