更改Java中的文件权限

huangapple go评论63阅读模式
英文:

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 &#39;false&#39;
            log.info { file?.canExecute() } // logs &#39;false&#39;
            file?.setExecutable(true) // still &#39;false&#39;
            file?.setWritable(true) // still &#39;false&#39;

            file.let {
                if (file != null) {
                    FileOutputStream(file).use { outputStream -&gt; 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

MultipartFiles不代表文件系统上的文件,而File代表您系统上的本地文件。它们只是上传的数据。

因此,multipartFile.originalFilename不会给您系统上任何文件的名称(除非存在一个与名称“随机”相同的文件),而是用户在其系统上上传的文件的名称。

如果您想将MultipartFile作为File访问,首先需要将其另存为:

val file = File("您想要上传到的目录/文件的名称")
multipartFile.transferTo(file)
//现在它已经存储在文件中

这将把文件复制到您的系统中。

英文:

MultipartFiles 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(&quot;what/ever/file/you.want&quot;)//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.

huangapple
  • 本文由 发表于 2023年2月6日 18:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定