spring-boot: url和uri
2018-12-27 10:33:04.813 INFO 61770 --- [http-nio-8080-exec-1] o.s.a.controller.DemoController :
uri = /Users/benjamin/opt/tomcat-8.5.37/webapps/access-resources-0.0.1-SNAPSHOT/WEB-INF/classes/static/one_3.jpg
2018-12-27 10:33:04.813 INFO 61770 --- [http-nio-8080-exec-1] o.s.a.controller.DemoController :
path = static/one_3.jpg
2018-12-27 10:33:04.813 INFO 61770 --- [http-nio-8080-exec-1] o.s.a.controller.DemoController :
file = /Users/benjamin/opt/tomcat-8.5.37/webapps/access-resources-0.0.1-SNAPSHOT/WEB-INF/classes/static/one_3.jpg
2018-12-27 10:33:04.813 INFO 61770 --- [http-nio-8080-exec-1] o.s.a.controller.DemoController :
url = file:/Users/benjamin/opt/tomcat-8.5.37/webapps/access-resources-0.0.1-SNAPSHOT/WEB-INF/classes/static/one_3.jpg
ClassPathResource resource = new ClassPathResource("static/one_3.jpg");
InputStream inputStream = resource.getInputStream();
String uri = "uri = " + resource.getURI().getPath();
String path = "path = " + resource.getPath();
String file = " file = " + resource.getFile().getAbsolutePath();
String url = "url = " + resource.getURL().toString();
logger.info(uri);
logger.info(path);
logger.info(file);
logger.info(url);
3. 获取资源
UrlResource urlResource = new UrlResource(img_01);
InputStream inputStream = urlResource.getInputStream();
4. classpath: 创建新的文件:
/**
*
* 1. 找到项目classes目录所在位置:
* 举例:/Users/benjamin/opt/tomcat-8.5.37/webapps/access-resources-0.0.1-SNAPSHOT/WEB-INF/classes
*
* 为了找到classes目录所在的位置: 在源码中添加 static/one_3.jpg
*
*
*
* 2。 创建文件:
*
*
*
*
*
* // 1。 获取当前文件的路径:
* String demoimg = "static/one_3.jpg";
* ClassPathResource tempResource = new ClassPathResource(demoimg);
* String path = tempResource.getURI().getPath();
* logger.info("host:" + path);
* String parentpath = path.substring(0, path.lastIndexOf(demoimg));
* logger.info("parentpath:" + parentpath);
*
* //2。 创建新的文件
* File newFile = new File(parentpath, "static/xxx.png");
* FileUtils.touch(newFile);
*
* FileOutputStream outputStream = new FileOutputStream(newFile);
*
*
*
*/
5. filesystem和classpath
// 1。 获取当前文件的路径:
String demoimg = "static/one_3.jpg";
ClassPathResource tempResource = new ClassPathResource(demoimg);
String path = tempResource.getURI().getPath();
logger.info("host:" + path);
String parentpath = path.substring(0, path.lastIndexOf(demoimg));
logger.info("parentpath:" + parentpath);
FileSystemResource fileSystemResource = new FileSystemResource(demoimg);
String osUri = fileSystemResource.getURI().toString();
logger.info("osPath:" + osUri);
classpath:
2018-12-27 11:20:45.839 INFO 62230 --- [nio-8080-exec-1] o.s.a.controller.DemoController : host:/Users/benjamin/idea-workspace/springboot-example/access-resources/target/classes/static/one_3.jpg
fileSystem:
2018-12-27 11:20:45.839 INFO 62230 --- [nio-8080-exec-1] o.s.a.controller.DemoController : osPath:file:/Users/benjamin/idea-workspace/springboot-example/get-started/static/one_3.jpg
运行在tomcat/webapps/项目
2018-12-27 11:26:26.567 INFO 62329 --- [http-nio-8080-exec-1] o.s.a.controller.DemoController :
host:/Users/benjamin/opt/tomcat-8.5.37/webapps/access-resources-0.0.1-SNAPSHOT/WEB-INF/classes/static/one_3.jpg
2018-12-27 11:26:26.568 INFO 62329 --- [http-nio-8080-exec-1] o.s.a.controller.DemoController : parentpath:/Users/benjamin/opt/tomcat-8.5.37/webapps/access-resources-0.0.1-SNAPSHOT/WEB-INF/classes/
2018-12-27 11:26:26.568 INFO 62329 --- [http-nio-8080-exec-1] o.s.a.controller.DemoController : osPath:file:/Users/benjamin/opt/tomcat-8.5.37/bin/static/one_3.jpg
spring-boot: resouces: 资源访问api
/* ClassPathResource resource = new ClassPathResource("static/one_3.jpg");
InputStream inputStream = resource.getInputStream();
String uri = "uri = " + resource.getURI().getPath();
String path = "path = " + resource.getPath();
String file = " file = " + resource.getFile().getAbsolutePath();
String url = "url = " + resource.getURL().toString();
logger.info(uri);
logger.info(path);
logger.info(file);
logger.info(url);*/
String img_01 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545888795792&di=099850428a3eda98ad94e85389c25196&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F9825bc315c6034a84d0cf125c6134954082376a3.jpg";
UrlResource urlResource = new UrlResource(img_01);
InputStream inputStream = urlResource.getInputStream();
// 1。 获取当前文件的路径:
String demoimg = "static/one_3.jpg";
ClassPathResource tempResource = new ClassPathResource(demoimg);
String path = tempResource.getURI().getPath();
logger.info("host:" + path);
String parentpath = path.substring(0, path.lastIndexOf(demoimg));
logger.info("parentpath:" + parentpath);
//2。 创建新的文件
File newFile = new File(parentpath, "static/xxx.png");
FileUtils.touch(newFile);
FileOutputStream outputStream = new FileOutputStream(newFile);
/* FileSystemResource fileSystemResource = new FileSystemResource(demoimg);
String osUri = fileSystemResource.getURI().toString();
logger.info("osPath:" + osUri);*/
/* ClassPathResource classPathResource = new ClassPathResource("static/xxx.jpg");
if (!classPathResource.exists()) {
logger.info("file abspath:" + classPathResource.getFile().getAbsolutePath());
logger.info("exist: " + classPathResource.exists());
}
*/
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
网友评论