我正在尝试通过spring framework将映像路径存储到mysql数据库中…但无法做到..:/

h43kikqp  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(329)
<h3>File to upload</h3>

<form action ="upload" method="post" enctype="multipart/form-data">
<input type ="file" name ="file">
<input type ="submit" value ="submit">
</form>

这是我的控制器

@RequestMapping(value="/upload",method=RequestMethod.POST)
public ModelAndView upload(@RequestParam("file") CommonsMultipartFile file,HttpSession session) throws IOException
{
    String path =  session.getServletContext().getRealPath("/");
    String filename=file.getOriginalFilename();
    ImagePOJO pojo = new ImagePOJO();
    byte barr[]=file.getBytes();    

    pojo.setPath(path);
    pojo.setFilename(filename);

    //String q = pojo.setPath(path)+"/"+pojo.setFilename(filename); 

    String w = pojo.getPath()+""+pojo.getFilename();
    //System.out.println(Arrays.toString(barr));

    System.out.println(path+" "+filename);

    System.out.println(w);

    BufferedOutputStream bout;
    try {
        bout = new BufferedOutputStream(new FileOutputStream(path+"/"+filename));

        Object o = bout;
        bout.write(barr);
        bout.flush();
        bout.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }[enter image description here][1]
js81xvg6

js81xvg61#

你为什么不试一试Spring的内容呢?
为自己生成一个spring启动应用程序http://start.spring.io/.
将这些依赖项添加到pom中。
pom.xml文件

<dependency>
    <groupId>com.github.paulcwarren</groupId>
    <artifactId>spring-content-fs-boot-starter</artifactId>
    <version>0.0.11</version>
</dependency>
<dependency>
    <groupId>com.github.paulcwarren</groupId>
    <artifactId>spring-content-rest-boot-starter</artifactId>
    <version>0.0.11</version>
</dependency>

添加 UploadStore 你的主要课程:
springbootapplication.java文件

@SpringBootApplication
public class SpringContentApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringContentApplication.class, args);
    }

    @StoreRestResource(path="upload")
    public interface UploadStore extends Store<String> {
    }
}

每个上传的文件都需要一个唯一的名称,所以让我们生成一个guid并动态设置表单的操作。按以下方式更新html:
表单.html

<script language="JavaScript">
    window.onload = function() {
        document.myform.action = get_action();
    }

    function get_action() {
        return "upload/" + guidGenerator();
    }

    function guidGenerator() {
        var S4 = function() {
           return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
        };
        return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
    }
</script>

<html>
    <body>
        <h3>File to upload</h3>

        <form name=myform method="post" enctype="multipart/form-data">
            <input type ="file" name ="file">
            <input type ="submit" value ="submit">
        </form>
    </body>
</html>

运行应用程序并为uploadstore提供一个位置:

java -jar yourapp.jar --spring.content.fs.filesystemRoot=/path/to/your/store
``` `UploadStore` 以及 `upload` 不是很好的名字,因为这是一个功能齐全的内容服务,支持post、put、get和delete。甚至支持视频流。
uploadstore也可以实现 `Renderable` 使您能够获取上传内容的格式副本;i、 例如,获取已上载word文档的pdf。
和/或 `Searchable` 启用全文索引(但这也需要apachesolr)。
它可以与spring数据相结合,允许您关联spring数据实体和spring内容资源。
hth公司

相关问题