简单的做个小Demo。
获取方式:
package com.example.file1.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.File; /** * @program: file1 * @Date: 2019/1/15 16:23 * @Author:懒惰的小妖 * @Description:1:获取classes下的所有文件。 */ @RestController public class FileController { @GetMapping("/path") public File[] listFile(){ //1.创建File对象,如果不加"/" File file = new File(this.getClass().getResource("").getPath()); //得到的是当前class文件的URI目录 //0 "E:\\SpringSecurity\\file1\\target\\classes\\com\\example\\file1\\controller\\FileController.class" return file.listFiles(); } }
在浏览器中看到:
如果: //2.创建File对象,如果加"/" 得到的是当前的classpath路径
File file = new File(this.getClass().getResource("/").getPath());
在浏览器中看到: