在Spring Boot中,静态资源默认放置在src/main/resources/static目录下。如果要将上传的文件放入该目录下并访问,可以按照以下步骤进行操作:

  1. application.propertiesapplication.yml文件中配置静态资源路径:

    • application.properties
      spring.resources.static-locations=classpath:/static/
      
    • application.yml
      spring:
        resources:
          static-locations: classpath:/static/
      
  2. 创建一个UploadController类来处理文件上传:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.StandardCopyOption;
    
    @Controller
    public class UploadController {
    
        @Value("${spring.resources.static-locations}")
        private String staticLocations;
    
        @PostMapping("/upload")
        public String uploadFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
            if (file.isEmpty()) {
                redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
                return "redirect:/";
            }
    
            try {
                // 获取文件名
                String fileName = StringUtils.cleanPath(file.getOriginalFilename());
                // 获取静态资源目录的路径
                Path staticPath = Path.of(staticLocations.substring("classpath:".length()));
                // 创建文件路径
                Path filePath = staticPath.resolve(fileName);
                // 复制文件到静态资源目录
                Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
                // 设置重定向消息
                redirectAttributes.addFlashAttribute("message", "You successfully uploaded '" + fileName + "'");
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return "redirect:/";
        }
    }
    
  3. UploadController中,uploadFile方法接收MultipartFile类型的参数,该参数用于接收上传的文件。在方法中,首先检查文件是否为空,然后获取文件名,然后获取静态资源目录的路径,接着创建文件路径,并将上传的文件复制到该路径下。

  4. 在HTML页面中添加文件上传表单和显示上传结果的代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>File Upload</title>
    </head>
    <body>
        <h2>File Upload</h2>
        <form method="post" action="/upload" enctype="multipart/form-data">
            <input type="file" name="file" />
            <button type="submit">Upload</button>
        </form>
        <p th:text="${message}"></p>
    </body>
    </html>
    

    在表单中,enctype="multipart/form-data"用于支持文件上传。

  5. 启动应用程序并访问http://localhost:8080,选择一个文件并点击上传按钮,上传成功后页面上会显示成功消息。上传的文件将保存在src/main/resources/static目录下,并可以通过http://localhost:8080/文件名进行访问

springboot 上传文件放入 static目录如何访问

原文地址: https://www.cveoy.top/t/topic/h33P 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录