英文:
Change file permissions in java
问题
要将`MultipartFile`转换为`File`并将其上传到`ec2`实例上的指定目录。以下是转换器方法。
```kotlin
private fun convertMultiPartFileToFile(multipartFile: MultipartFile, name: String): File? {
val file = multipartFile.originalFilename?.let { File(it) }
try {
log.info { file?.canWrite() } // 记录 'false'
log.info { file?.canExecute() } // 记录 'false'
file?.setExecutable(true) // 仍然 'false'
file?.setWritable(true) // 仍然 'false'
file.let {
if (file != null) {
FileOutputStream(file).use { outputStream -> outputStream.write(multipartFile.bytes) }
}
}
} catch (e: IOException) {
throw RuntimeException(e)
}
return file
}
在本地工作正常,但在我的ec2
实例上尝试时,出现错误
java.lang.RuntimeException: java.io.FileNotFoundException: file.png (权限被拒绝)
我猜这是因为我没有权限写入指定文件。如果file?.setWritable(true)
和file?.setExecutable(true)
返回false,我该如何解决这个问题?
我通过java -jar path/to/jar
运行jar文件。
setWritable
方法的文档说明如下:
* @return {@code true} 当且仅当操作成功。如果用户无法更改此抽象路径名的访问权限,操作将失败。
那么如何获取对此抽象路径名的访问权限?
<details>
<summary>英文:</summary>
I want to convert the `MultipartFile` to `File` and upload this to specified directory on my `ec2` instance. Here is the converter method.
```kotlin
private fun convertMultiPartFileToFile(multipartFile: MultipartFile, name: String): File? {
val file = multipartFile.originalFilename?.let { File(it) }
try {
log.info { file?.canWrite() } // logs 'false'
log.info { file?.canExecute() } // logs 'false'
file?.setExecutable(true) // still 'false'
file?.setWritable(true) // still 'false'
file.let {
if (file != null) {
FileOutputStream(file).use { outputStream -> outputStream.write(multipartFile.bytes) }
}
}
} catch (e: IOException) {
throw RuntimeException(e)
}
return file
}
It works locally, but when I try this on my ec2
instance, I got the error
java.lang.RuntimeException: java.io.FileNotFoundException: file.png (Permission denied)
I guess it's because I do not have permission to write to specified file. How can I solve this if file?.setWritable(file)
and file?.setExecutable(true)
return false.
I run the jar by java -jar path/to/jar
The setWritable
method documentation says
* @return {@code true} if and only if the operation succeeded. The
* operation will fail if the user does not have permission to
* change the access permissions of this abstract pathname.
How to get the access permissions to this abstract pathname then?
答案1
得分: 1
MultipartFile
s不代表文件系统上的文件,而File
代表您系统上的本地文件。它们只是上传的数据。
因此,multipartFile.originalFilename
不会给您系统上任何文件的名称(除非存在一个与名称“随机”相同的文件),而是用户在其系统上上传的文件的名称。
如果您想将MultipartFile
作为File
访问,首先需要将其另存为:
val file = File("您想要上传到的目录/文件的名称")
multipartFile.transferTo(file)
//现在它已经存储在文件中
这将把文件复制到您的系统中。
英文:
MultipartFile
s do not represent files on the file system while File
represents a local file on your system. They are just data that's uploaded.
Hence, multipartFile.originalFilename
does not give you the name of any file on your system (except a file with the same name "randomly" exists) but the name of the file the user uploaded (on their system).
If you want to access a MultipartFile
as a File
, you first need to save it as such:
val file=File("what/ever/file/you.want")//in the directory you want to upload it to
multipartFile.transferTo(file)
//it is now stored in file
This will copy the file to your system.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论