Idea下获取项目中图片资源文件路径(也就是项目所在服务器的磁盘绝对路径)。realPath:request.getServletContext().getRealPath("");

您所在的位置:网站首页 怎么查看图片路径地址 Idea下获取项目中图片资源文件路径(也就是项目所在服务器的磁盘绝对路径)。realPath:request.getServletContext().getRealPath("");

Idea下获取项目中图片资源文件路径(也就是项目所在服务器的磁盘绝对路径)。realPath:request.getServletContext().getRealPath("");

2024-07-13 13:42| 来源: 网络整理| 查看: 265

=====###【和eclipse不一样:Idea下资源文件也会编译(到artifact目录下)。

eclipse下不会编译资源文件(不确定,记得是这样。)】

本以为 和eclipse一样,realPath是在webRoot根目录下。所以拼接的图片 绝对路径总是不对。 以为是方法用的不对。各种百度,试了各种方法。费时将近5个小时。接近12:00吃了碗泡面。回来debug一下,终于看到realPath的值。恍然大悟。 之前没有debug realPath的值。因为看到contextPath的值为空串。和eclipse不一样。就【一直以为是SSM里Servlet API不能用(照样能用!底层代码当然可以!!!)】。所以也直接没有看realPath的值。

 好了洗洗睡吧。debug。下班后思路疲惫。脑子转的慢了。只有靠勤奋找思路。呜呜呜。。。

====走过的弯路:

@Override public int insertUser(UserDO user, String rePassword,HttpServletRequest request) { int userMessageIsAvailable = this.checkUserMessage(user, rePassword); if (userMessageIsAvailable != 1) { return userMessageIsAvailable; } String salt = Long.toString(System.currentTimeMillis()); ByteSource byteSource = ByteSource.Util.bytes(salt); user.setSalt(salt); Object md5Password = new SimpleHash("MD5", user.getPassword(), byteSource, 5); String password = user.getPassword();//设置加密密码前,取出明文密码。发邮件 user.setPassword(String.valueOf(md5Password)); int a = userMapper.insertSelective(user); if( new Integer(a) !=null ) { new Thread(){ @Override public void run() { //========================》freeMarker创建邮件内容:开始 Configuration configuration = freeMarkerConfigurer.getConfiguration(); //加载模板对象 Template template = null; try { template = configuration.getTemplate("mail.ftl"); //创建一个数据集 Map data = new HashMap(); //data需要的数据分析:图片绝对路径。 //拿到当前服务器的ip和端口 String localAddr = request.getLocalAddr(); int localPort = request.getLocalPort(); String contextPath = request.getContextPath();//空串:"" String realPath = request.getServletContext().getRealPath(""); System.out.println("==========》realPath:"+realPath);//E:\ClickCube\classes\artifacts\ClickCube_front_Web_exploded\ //String logoUrl="http://"+localAddr+":"+localPort+contextPath+"/resource/mailLogo/mailLogo.png";//必须加http // logoUrl的IP不对。换个设备就访问不到Logot图片了。IP总是服务器本机的127.0.0.1 //解决思路: // ①图片写到浏览器。 // ②拿到正确的服务器IP【同一台服务器。网络变化:服务器IP变化(所以只能发送logo附件。本地显示附件太麻烦)】 // ③logo图片放到 fastDFS(稳定的文件服务器)上,再访问远程的图片。 //防止图片位置变化,每次发邮件,上传一次mailLogo.png // String relativeLogoUrl=contextPath+"\\resource\\mailLogo\\mailLogo.png";//\resource\mailLogo\mailLogo.png (系统找不到指定的路径。) // String relativeLogoUrl="\\"+contextPath+"\\resource\\mailLogo\\mailLogo.png";//\\resource\mailLogo\mailLogo.png (找不到网络路径。) //this.getServletContext().getRealPath() // ServletContext servletContext = new ApplicationContext(new StandardContext() ); // String relativeLogoUrl=servletContext.getRealPath("\\resource\\mailLogo\\mailLogo.png");//\\resource\mailLogo\mailLogo.png (找不到网络路径。) //Resource res = new ClassPathResource("\\resource\\mailLogo\\mailLogo.png");//类路径 // Resource res = new ClassPathResource("classpath:resource\\mailLogo\\mailLogo.png");// class path resource [classpath:resource/mailLogo/mailLogo.png] cannot be resolved to URL because it does not exist //Resource res = new ClassPathResource("classpath:resource/mailLogo/mailLogo.png");// class path resource [classpath:resource/mailLogo/mailLogo.png] cannot be resolved to URL because it does not exist // Resource res3 = new ClassPathResource("file:resource\\mailLogo\\mailLogo.png");// class path resource [file:resource/mailLogo/mailLogo.png] cannot be resolved to URL because it does not exist // Resource res = new ClassPathResource("mailLogo.png");// class path resource [mailLogo.png] cannot be opened because it does not exist //Resource res2 = new ClassPathResource("classpath:mailLogo.png");//[classpath:mailLogo.png] cannot be resolved to URL because it does not exist // Resource res = new ClassPathResource("file:mailLogo.png"); //class path resource [file:mailLogo.png] cannot be opened because it does not exist //1 // InputStream inputStream = ClassLoader.getSystemResourceAsStream("mailLogo.png");//null。 参数就是带后缀 //2 // FileInputStream inputStream = new FileInputStream("mailLogo.png");// mailLogo.png (系统找不到指定的文件。) //3 // Enumeration resourceUrls = Thread.currentThread().getContextClassLoader().getResources("mailLogo.png"); // while (resourceUrls.hasMoreElements()) { // URL url = (URL) resourceUrls.nextElement(); // System.out.println("==========》url:"+url); // inputStream = url.openStream(); // if(inputStream!=null){ // break; // } // } InputStream inputStream = new FileInputStream(realPath+"resource/mailLogo/mailLogo.png");// //不行换个MultipartFile的实现类试试======》 MockMultipartFile multipartFile = new MockMultipartFile("mailLogo.png", inputStream); // String fileName = multipartFile.getName(); //businessLicenseFileId等4个XXXFileId是要存到数据库中的绝对ID,或者相对路径,也是在服务器中的相对地址 String fileId = FastDFSUtil.uploadFile(multipartFile.getBytes(), fileName); //获取服务器写在配置文件中的头地址 String filePathUrl = ReadPropertiesUtil.readFilePath(); //设置在服务器上的绝对地址 String logoUrl = filePathUrl + "/" + fileId; System.out.println("=====》logoUrl:" + logoUrl); data.put("logoUrl", logoUrl);//ok data.put("loginName", user.getLoginName()); data.put("password", password);//加密前取出的明文密码。 /* //指定文件输出的路径及文件名 File htmlFile = new File("D:\\freemarker_registerMail\\mail.html"); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile),"UTF-8")); //输出文件 template.process(data, out);//ok //关闭流 out.close();*/ //========================》freeMarker创建邮件内容:结束 String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, data); String toMail = user.getEmail(); MailUtils.sendMail("注册邮件", content, toMail); } catch (Exception e) { e.printStackTrace(); } }//run线程体 }.start(); }//a==1 插入记录成功。 return user.getId(); }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3