赞
踩
uni-app图片读取只能从静态资源或者使用网络图片
如果想使用本地图片的话需要将路径映射到磁盘路径中去。这里我们在properties中写:
- filePath=D:/temp/images/
- spring.resources.static-locations=classpath:static/**spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${filePath}
后台代码
- @RequestMapping("getimg")
- @ResponseBody
- public String getimg(HttpServletRequest request,@RequestParam MultipartFile file){
- //获取图片名称
- String fname = file.getOriginalFilename();
- //获取图片名称后缀 .png 或 .jpg
- String[] split = fname.split("\\.");
- //图片存储路径 Linux注意不能带冒号:
- String filepath="D:\\temp\\images\\";
- //↓没有横杠的uuid 自行百度
- //String uuid = UtilTool.getUUid();
- UUID uuid = UUID.randomUUID();
- //随机文件名
- String finalname=uuid + "." + split[1];
-
- //最终路径
- String finalpath=filepath+finalname;
-
-
-
- try {
- file.transferTo(new File(finalpath));
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- return "http://"+request.getRemoteHost()+":"+request.getServerPort()+"/"+finalname;
-
- }

链接格式如下
常见问题总结
1) 不可以将上传来的文件保存到项目地址中,即不可以保存在resource/static目录下,因为这样的话,及时上传成功,也需要重启整个项目才能够通过地址访问到该图片。换言之,target不会因为你上传了图片,就自动更新静态资源。
2) 配置文件中static-location后面配置的那些目录,就是localhost:8080可以直接访问的文件了,也就是说我们现在已经将磁盘的目录配置上了,那么我们存放在E:/Data/uploadFile文件夹中的任何文件就可以直接localhost:8080/文件名进行访问了。
看了这个大哥写的文章总结的 原文链接:https://blog.csdn.net/yiyexy/article/details/105016786
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。