Skip to content

Commit dd53dd3

Browse files
committed
test ResourceLoader
1 parent 6900da4 commit dd53dd3

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

help/http-requests.http

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ Content-Type: application/json
4949
"password": "your-name",
5050
"age": 30
5151
}
52+
53+
### /resourceloader/staticpaths
54+
GET http://localhost:9100/resourceloader/staticpaths
55+
56+
### /resourceloader/staticpaths/physicalpath
57+
GET http://localhost:9100/resourceloader/staticpaths/physicalpath
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.netbuffer.springboot.demo.controller;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.core.io.ResourceLoader;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import javax.annotation.Resource;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
@Slf4j
15+
@RestController
16+
@RequestMapping("/resourceloader")
17+
public class ResourceLoaderController {
18+
19+
@Resource
20+
private ResourceLoader resourceLoader;
21+
22+
@Value("${spring.web.resources.static-locations}")
23+
private String staticLocations;
24+
25+
@GetMapping("staticpaths")
26+
public List<String> staticPaths() {
27+
String[] locations = staticLocations.split(",");
28+
return Arrays.asList(locations);
29+
}
30+
31+
@GetMapping("staticpaths/physicalpath")
32+
public List<String> staticPathsPhysicalPath() {
33+
List list = new ArrayList();
34+
List<String> locations = staticPaths();
35+
for (String location : locations) {
36+
try {
37+
list.add(resourceLoader.getResource(location.trim()).getFile().getAbsolutePath());
38+
} catch (Exception e) {
39+
log.error("getResource[" + location + "] error:", e);
40+
}
41+
}
42+
return list;
43+
}
44+
45+
}

src/main/resources/application.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ spring:
2424
key: test-actuator
2525
web:
2626
resources:
27-
static-locations: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/, file:d:/upload
27+
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:d:/upload
2828

2929
management:
3030
info:

0 commit comments

Comments
 (0)