Java 我可以从Spring引导应用程序将文件写入CentOS usr/share/files/吗?

Java 我可以从Spring引导应用程序将文件写入CentOS usr/share/files/吗?,java,spring-boot,tomcat,centos,Java,Spring Boot,Tomcat,Centos,我正在写一个文件上传的Spring boot应用程序,除了路径之外的所有东西都很好用。 我在窗户上测试过,效果很好。但是当我将war包上传到tomcat时,上传看起来很成功。但我在服务器上的任何地方都找不到该文件 private const val UPLOADED_FOLDER = "usr/share/nginx/html/" @PostMapping("/upload.do") @ResponseBody fun singleFil

我正在写一个文件上传的Spring boot应用程序,除了路径之外的所有东西都很好用。 我在窗户上测试过,效果很好。但是当我将war包上传到tomcat时,上传看起来很成功。但我在服务器上的任何地方都找不到该文件

private const val UPLOADED_FOLDER = "usr/share/nginx/html/"

@PostMapping("/upload.do")
    @ResponseBody
    fun singleFileUpload(@RequestParam("file") file: MultipartFile,
                         @RequestParam("path") path: String? = "",
                         redirectAttributes: RedirectAttributes): ResponseEntity<*> {
        if (file.isEmpty) {
            redirectAttributes.addFlashAttribute("message", "Please select a file to upload")
            return ResponseEntity.ok("redirect:uploadStatus")
        }
        val pathRoot = UPLOADED_FOLDER + path
        try { // Get the file and save it somewhere
            val bytes = file.bytes
            val path: Path =
                    Paths.get(pathRoot + File.separator + file.originalFilename)
            Files.write(path, bytes)
            redirectAttributes.addFlashAttribute("message",
                    "You successfully uploaded '" + file.originalFilename + "'")
        } catch (e: IOException) {
            e.printStackTrace()
        }
        fileCompressService.decompressTar(pathRoot, file.originalFilename)

        return ResponseEntity.ok("redirect:uploadStatus")
    }
private const val上传\u FOLDER=“usr/share/nginx/html/”
@PostMapping(“/upload.do”)
@应答器
fun singleFileUpload(@RequestParam(“文件”)文件:MultipartFile,
@RequestParam(“路径”)路径:字符串?=“”,
重定向属性:重定向属性):响应属性{
if(file.isEmpty){
redirectAttributes.addFlashAttribute(“消息”,“请选择要上载的文件”)
返回ResponseEntity.ok(“重定向:上传状态”)
}
val pathRoot=上传的文件夹+路径
尝试{//获取文件并将其保存到某个位置
val bytes=file.bytes
val路径:路径=
path.get(pathRoot+File.separator+File.originalFilename)
文件。写入(路径,字节)
redirectAttributes.addFlashAttribute(“消息”,
“您已成功上载”“+file.originalFilename+”))
}捕获(e:IOException){
e、 printStackTrace()
}
fileCompressService.decompressTar(路径根,文件.originalFilename)
返回ResponseEntity.ok(“重定向:上传状态”)
}

您使用相对路径
usr/share/nginx/html
,这意味着您的文件写入服务器工作目录的子目录中。这可能因服务器而异,但可能是
$CATALINA\u BASE
。使用绝对路径
/usr/share/nginx/html

备注:在UNIX系统上,您可能会被拒绝权限,因为
/usr
只能由
root
写入。变量数据通常应写入
/var