http状态403-上传文件时禁止-spring boot applicate

rdrgkggo  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(251)

我正在使用springbootmultipartfile来允许用户上传他们的文件,并且我想将上传的文件保存在project目录中(或者本地工作的任何地方)。当我提交任何文件,我得到403错误禁止(springbootstarter安全性用于嵌入式登录)。
以下是浏览器中打印的错误截图:403禁止
在控制台中,出现以下错误:
org.thymeleaf.exceptions.templateinputexception:解析模板[error]时出错,模板可能不存在,或者任何已配置的模板解析程序都无法访问模板
我学习了以下教程:https://mkyong.com/spring-boot/spring-boot-file-upload-example/
这是我的项目结构:项目结构
这是我的上传控制器:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class UploadController {

//Save the uploaded file to this folder
private static String UPLOADED_FOLDER = "./src/main/resources/uploaded";

@GetMapping("/test")
public String index() {
    return "upload";
}

@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() {
    return "uploadStatus";
}

}

这里是upload.html:

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

<h1>You can upload your files here</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>

uploadstatus.html:

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

<h1>Upload Status</h1>

<div th:if="${message}">
<h2 th:text="${message}"/>
</div>

</body>
</html>

我使用thymeleaf依赖项:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

我还是一个初学者,对spring boot有基本的经验,我一直在寻找解决方案,所以请任何帮助/建议,以解决问题将不胜感激。
提前谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题