springboot+thymeleaf通过表单上传文件时报java.io.FileNotFoundException的错误解决方案

当提交表单时,不上传文件后台能够拿到表单中的数据,但是带上文件后就报java.io.FileNotFoundException的错误。

表单如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script type="text/javascript" th:src="@{/static/js/jquery-3.3.1.min.js}"></script>
    <script type="text/javascript" th:src="@{/static/js/jquery-form.js}"></script>
    <title>文件上传</title>
</head>
<body>

<form id="form" method="post" enctype="multipart/form-data" th:action="@{/getUser}">

    <div>
        <label>姓名</label>
        <input type="text" name="name" />
    </div>
    <div>
        <label>性别</label>
        <input type="text" name="gender" />
    </div>
    <div>
        <label>文件</label>
        <input type="file" name="file" value="文件上传" />
    </div>
    <div>
        <button id="bt">提交</button>
    </div>

</form>

</body>
<script>
    $(document).ready(function(){
        $("#bt").click(function(){
            $("#form").ajaxForm({
                type:"post",
                url:"./getUser",
                success:function(result){
                    alert("sdfsd");
                }
            });
        });
    });
</script>

</html>

user类:

package com.lrq.demo.model;

import java.io.Serializable;
import java.sql.Blob;
import java.util.Objects;

public class User implements Serializable {
    private Integer id;
    private String name;
    private String idNo;
    private String gender;
    private Blob file;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Blob getFile() {
        return file;
    }

    public void setFile(Blob file) {
        this.file = file;
    }

}

controller(仅用作测试)如下:

找了好久发现表单中的上传的文件名称属性就可以获取到了比如将

<input type="file" name="file" value="文件上传" />修改为
<input type="file1" name="file" value="文件上传" />

提交表单的时候就不会报错了。这时候有人肯定会说在参数中加入

@RequestParam("file") MultipartFile file

这个方法我也试过会报java.io.FileNotFoundException的错误。

以上是我个人遇到的错误的解决方案,如果有错误欢迎批评指正,或者有更好的方法也欢迎赐教!

发布了11 篇原创文章 · 获赞 1 · 访问量 6822

猜你喜欢

转载自blog.csdn.net/u010808777/article/details/100176435
今日推荐