java Spring Boot 应用的应用根路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43846093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:45:44  来源:igfitidea点击:

Application Root Path of Spring Boot application

javaspringfile-uploadspring-boot

提问by Bahadar Ali

This answeris not a standard way to get application root path (file system path). I need to upload a file to a directory e.g. uploads created in application main directory. How can I get the application root path in my Java Class. I am developing Rest API. Please help in this regard.

答案不是获取应用程序根路径(文件系统路径)的标准方法。我需要将文件上传到一个目录,例如在应用程序主目录中创建的上传。如何在我的 Java 类中获取应用程序根路径。我正在开发 Rest API。请在这方面提供帮助。

回答by fiskra

If I understand right, you want to develop a REST API which aims to upload a file to directory that located in your application directory. It's recommended to create file,images,.. in resources directory. And basically you should be using servlet context to get the absolute path of this directory. First you need servletContext

如果我理解正确,您想开发一个 REST API,旨在将文件上传到位于您的应用程序目录中的目录。建议在资源目录中创建文件、图像、..。基本上你应该使用 servlet 上下文来获取这个目录的绝对路径。首先你需要servletContext

@Autowired
ServletContext context;

Then you can get absolute and relative directory("resources/uploads"):

然后你可以得到绝对和相对目录(“资源/上传”)

String absolutePath = context.getRealPath("resources/uploads");
File uploadedFile = new File(absolutePath, "your_file_name");

EDITED:

编辑:

You would like to develop rest api. So you can create a controller class first

你想开发rest api。所以你可以先创建一个控制器类

@RestController
@RequestMapping("/file")
public class FileController {

@Autowired
ServletContext context;

@PostMapping("/upload") 
public String fileUpload(@RequestParam("file") MultipartFile file) {

    if (file.isEmpty()) {
       throw new RuntimeException("Please load a file");
    }

    try {

        // Get the file and save it uploads dir
        byte[] bytes = file.getBytes();
        Path path = Paths.get(context.getRealPath("uploads") + file.getOriginalFilename());
        Files.write(path, bytes);

    } catch (IOException e) {
        e.printStackTrace();
    }

    return "success";
}

}

There is another way to manage file operations and Spring Boot documentation explained very well: Uploading Files Spring Boot

还有另一种管理文件操作的方法,Spring Boot 文档解释的很好:Uploading Files Spring Boot

回答by Aniket Sahrawat

You can get the root directory of your application using System.getProperty("user.dir"). This works with properties, ymlor pom.xmlfiles as well.

您可以使用System.getProperty("user.dir"). 这也适用于properties,ymlpom.xml文件。

yml:

yml

info:
  working-dir: ${user.dir}

properties:

properties

info.working-dir=${user.dir}

pom.xml:

pom.xml

<properties> <!-- not really sure if anyone would ever do it -->
    <another.project.dir>${user.dir}/../someproject</another.project.dir>
</properties>

In some class:

在某些课程中:

System.out.println(System.getProperty("user.dir"));