当前位置:   article > 正文

uni-app图片读取问题_uniapp读取本地图片

uniapp读取本地图片

uni-app图片读取只能从静态资源或者使用网络图片

如果想使用本地图片的话需要将路径映射到磁盘路径中去。这里我们在properties中写:

  1. filePath=D:/temp/images/
  2. spring.resources.static-locations=classpath:static/**spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${filePath}

后台代码

  1. @RequestMapping("getimg")
  2. @ResponseBody
  3. public String getimg(HttpServletRequest request,@RequestParam MultipartFile file){
  4. //获取图片名称
  5. String fname = file.getOriginalFilename();
  6. //获取图片名称后缀 .png 或 .jpg
  7. String[] split = fname.split("\\.");
  8. //图片存储路径 Linux注意不能带冒号:
  9. String filepath="D:\\temp\\images\\";
  10. //↓没有横杠的uuid 自行百度
  11. //String uuid = UtilTool.getUUid();
  12. UUID uuid = UUID.randomUUID();
  13. //随机文件名
  14. String finalname=uuid + "." + split[1];
  15. //最终路径
  16. String finalpath=filepath+finalname;
  17. try {
  18. file.transferTo(new File(finalpath));
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. return "http://"+request.getRemoteHost()+":"+request.getServerPort()+"/"+finalname;
  23. }

链接格式如下

 常见问题总结
1) 不可以将上传来的文件保存到项目地址中,即不可以保存在resource/static目录下,因为这样的话,及时上传成功,也需要重启整个项目才能够通过地址访问到该图片。换言之,target不会因为你上传了图片,就自动更新静态资源。
2) 配置文件中static-location后面配置的那些目录,就是localhost:8080可以直接访问的文件了,也就是说我们现在已经将磁盘的目录配置上了,那么我们存放在E:/Data/uploadFile文件夹中的任何文件就可以直接localhost:8080/文件名进行访问了。
 

 

看了这个大哥写的文章总结的  原文链接:https://blog.csdn.net/yiyexy/article/details/105016786

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/758929
推荐阅读