0

我正在尝试使用以下表单上传文件:

<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
    <input type="file" name="files">
    <input type="submit" value="Upload Files" accept="image/png, image/jpeg"></input>
</form>

这是我的控制器:

private final String UPLOAD_DIR = "./uploads/";

@PostMapping("/upload")
    public String upload(@RequestParam("files") MultipartFile file, RedirectAttributes attributes) {
        
        if(file.isEmpty()) {
             attributes.addFlashAttribute("message", "Please select a file to upload.");
             return "redirect:/";
        }
        
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
        
        try {
            Path path = Paths.get(UPLOAD_DIR, fileName);
            Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        attributes.addFlashAttribute("message", "You successfully uploaded " + fileName + '!');
        
        return "redirect:/";
    }

在本地主机(windows)上一切正常,但我的问题是它在服务器(linux)上不起作用,因为我得到一个异常:“java.nio.file.FileSystemException:./uploads/file.png:只读文件系统”“上传”文件夹具有所有权限,请帮助,我已经没有想法了。

4

0 回答 0