菜鸟的springboot项目图片上传及图片路径分析 |
您所在的位置:网站首页 › 图片路径不存在什么意思 › 菜鸟的springboot项目图片上传及图片路径分析 |
菜鸟的springboot项目图片上传及图片路径分析
说明一、图片路径分析二、实现图片上传(1)单文件上传(非异步)(2)单文件上传(异步)
三、总结四、更新配置文件
说明
更新时间:2020/6/15 16:15,更新了部署环境的判断 本文记录一下springboot项目的图片上传的相关知识,主要解决项目打成jar包部署时的图片路径问题,本文会持续更新,不断地扩充 本文仅为记录学习轨迹,如有侵权,联系删除 一、图片路径分析springboot项目在还没打包时,很多人喜欢把图片上传后,保存在项目的静态资源下,就像下面的图片那样 我们知道项目打包后的jar包都在target文件夹里面,也就是说target所在文件夹才是jar包所在的路径,所以,图片存储的位置就应该在target里面,这样在打成jar包后,就可以获取jar包所在目录,实现上面分析的功能 前端代码 单文件上传 上传成功 单文件上传后端对应控制器的代码 //来到单文件上传页面(非异步) @GetMapping("/toSingleUpload") public String singleUpload() throws FileNotFoundException { return "singleUpload"; } //上传图片(非异步) @PostMapping("/singleUploadFile") public String singleUploadFile(MultipartFile file, Model model){ String fileName=file.getOriginalFilename(); //获取文件名以及后缀名 //fileName= UUID.randomUUID()+"_"+fileName;//重新生成文件名(根据具体情况生成对应文件名) //获取jar包所在目录 ApplicationHome h = new ApplicationHome(getClass()); File jarF = h.getSource(); //在jar包所在目录下生成一个upload文件夹用来存储上传的图片 String dirPath = jarF.getParentFile().toString()+"/upload/"; System.out.println(dirPath); File filePath=new File(dirPath); if(!filePath.exists()){ filePath.mkdirs(); } try{ //将文件写入磁盘 file.transferTo(new File(dirPath+fileName)); //上传成功返回状态信息 model.addAttribute("uploadStatus","上传成功"); }catch (Exception e){ e.printStackTrace(); //上传失败,返回失败信息 model.addAttribute("uploadStatus","上传失败:"+e.getMessage()); } //携带上传状态信息回调到文件上传页面 return "singleUpload"; }
在(1)非异步单文件上传的基础,再实现异步单文件上传 前端代码 无刷文件上传 上传成功 无刷文件上传 function ajaxUpload() { var form=new FormData(); //获取选择的文件 $.each($('input[name="files"]'),function (index,item) { form.append("files",item.files[0]) }); //发送异步请求 $.ajax({ method:'post', url:'/ajaxUploadFile', data:form, processData: false, contentType:false, success:function (res) { //成功返回触发的方法 alert(res.msg); }, //请求失败触发的方法 error:function () { console.log("ajax请求失败"); } }) }后端代码 //来到单文件上传页面(异步) @GetMapping("/ajaxUpload") public String ajaxUpload(){return "ajaxUpload";} //文件上传管理(异步) @PostMapping("/ajaxUploadFile") @ResponseBody public Map ajaxUploadFile(MultipartFile[] files){ Map map=new HashMap(); for(MultipartFile file:files){ //获取文件名以及后缀名 String fileName=file.getOriginalFilename(); //获取jar包所在目录 ApplicationHome h = new ApplicationHome(getClass()); File jarF = h.getSource(); //在jar包所在目录下生成一个upload文件夹用来存储上传的图片 String dirPath = jarF.getParentFile().toString()+"/upload/"; System.out.println(dirPath); File filePath=new File(dirPath); if(!filePath.exists()){ filePath.mkdirs(); } try{ //将文件写入磁盘 file.transferTo(new File(dirPath+fileName)); //文件上传成功返回状态信息 map.put("msg","上传成功!"); }catch (Exception e){ e.printStackTrace(); //上传失败,返回失败信息 map.put("msg","上传失败!"); } } //携带上传状态信息回调到文件上传页面 return map; }
(1)图片不能存储在项目里面的静态资源里面 (2)图片存储的位置在jar包所在目录,没打jar包时是target下的目录位置,打jar包后是jar所在目录(动态) (3)图片存储路径的获取用以下代码,获取的路径是target的目录 //获取jar包所在目录 ApplicationHome h = new ApplicationHome(getClass()); File jarF = h.getSource(); //在jar包所在目录下生成一个upload文件夹用来存储上传的图片 String dirPath = jarF.getParentFile().toString()+"/upload/"; System.out.println(dirPath); 四、更新配置文件更新了部署环境的判断,jar包部署可以有Linux环境和window环境,下面增加了环境的判断 @Configuration public class MyWebMvcConfig extends WebMvcConfigurerAdapter { //将jar文件下的对应静态资源文件路径对应到磁盘的路径(根据个人的情况修改"file:static/"的static的值 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { ApplicationHome h = new ApplicationHome(getClass()); File jarF = h.getSource(); String dirPath = jarF.getParentFile().toString()+"/upload/"; String os = System.getProperty("os.name"); System.out.println(os); if (os.toLowerCase().startsWith("win")) { //如果是Windows系统 registry.addResourceHandler("/upload/**").addResourceLocations("file:"+dirPath); System.out.println("file:" + dirPath); } else { //linux 和mac // registry.addResourceHandler("/upload/**") //虚拟路劲 // .addResourceLocations("file:" + System.getProperty("user.dir") + "/upload/");//jar 同级目录 // System.out.println("file:" + System.getProperty("user.dir") + "/upload/"); registry.addResourceHandler("/upload/**").addResourceLocations("file:"+dirPath); System.out.println("file:" + dirPath); } } } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |