Hibernate Spring引导读取用户上传的csv文件

Hibernate Spring引导读取用户上传的csv文件,hibernate,spring-boot,thymeleaf,Hibernate,Spring Boot,Thymeleaf,在我的系统中,我需要上传一个CSV文件并将详细信息保存在数据库中。作为第一步,我将上传的文件保存在一个选定的文件夹中。但是,当我尝试此操作时,我收到以下错误消息。(错误显示在图像中)。有人能帮我解决这个问题吗?多谢各位 对象控制器 @Controller public class SubjectController { @Autowired private SubjectDAO subjectDAO; @Autowired private CourseDAO

在我的系统中,我需要上传一个CSV文件并将详细信息保存在数据库中。作为第一步,我将上传的文件保存在一个选定的文件夹中。但是,当我尝试此操作时,我收到以下错误消息。(错误显示在图像中)。有人能帮我解决这个问题吗?多谢各位

对象控制器

@Controller
public class SubjectController {
    @Autowired
    private SubjectDAO subjectDAO;

    @Autowired
    private CourseDAO courseDAO;

    @Autowired
    private LectureHallDAO lectureHallDAO;

    private final SubjectRepository subjectRepository;



    public SubjectController(SubjectRepository subjectRepository) {
        this.subjectRepository = subjectRepository;
    }


    //Save the uploaded file to this folder
    private static String UPLOADED_FOLDER = "F://temp//";

    @GetMapping("/sub")
    public String index() {
        return "addAllSubject";
    }

    @PostMapping("/upload") // //new annotation since 4.3
    public String singleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {

        if (file.isEmpty()) {
            //redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
            return "redirect:uploadStatus";
        }

        try {
            // Get the file and save it somewhere
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);

            //redirectAttributes.addFlashAttribute("message",
            //        "You successfully uploaded '" + file.getOriginalFilename() + "'");

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "redirect:/uploadStatus";
    }

    @GetMapping("/uploadStatus")
    public String uploadStatus() {
        System.out.println("error");
        return "uploadStatus";
    }
}

addAllSubjects html文件

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>

<h1>Spring Boot file upload example</h1>

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

</body>
</html>

Spring启动文件上载示例



通过向WebSecurity配置中添加以下代码解决了此问题

@Override
    protected void configure(HttpSecurity http) throws Exception {         
        //to upload
        http.cors().and().csrf().disable();
    }

你不应该在没有正当理由和风险评估的情况下禁用cors。检查

特别是,鉴于thymeleaf将以非常简单的方式在您的表单中添加csrf令牌;只需为
th:action
更改表单的
action
属性即可。这将创建一个带有令牌的隐藏输入,该令牌将与您的表单一起提交,从而成功地发出POST请求

这样做与自己添加输入的效果相同(但在您的案例中没有理由这样做):


非常感谢。它工作得非常好,而且了解了cors@Ayesh17没问题,祝你好运!
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />`
var token = '[[${_csrf.token}]]';
var header = '[[${_csrf.headerName}]]';

$.ajax({
    beforeSend: function(xhr) {
      xhr.setRequestHeader(header, token);
    },
    ....
})