路径!! /*WebRoot --Image --ma.png 我在包下com.name.query下访问ma.png路径*/File imgFile = new File("/Image/ma.png"); //这上面路径怎么不对 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 路径不是这样用的,先用 class.getresources等方法获得当前工程的相对路径,然后再 拼接你这里的 /image/ma.png 建议jar中的资源和jar一起打包外部的资源就直接在页面上使用相对路径 File imgFile = new File("/Image/ma.png"); 时楼上说的很对,这里的路径得用文件在服务器上的绝对路径。取绝对路径的方法网上查查就有了。 File imgFile = new File("/Image/ma.png"); 这是从当前盘符下开始。去掉/是从当前工程目录下开始找。 你可以获得当前工程的相对路径,然后再拼接,如果就是在当前工程里使用File imgFile = new File("Image/ma.png"); //就算在工程下也不行!File imgFile = new File("Image/ma.png");//class.getresources 怎么取当前工程路径! getServlet().getServletContext().getRealPath("/")获得当前服务器觉得路径,如:d:/tomcat6/webapps/testdemo如果你的ma.png在工程image下那就是getServlet().getServletContext().getRealPath("/")+"Image/ma.png" 如果你要将“/Image/ma.png”放在Web应用上下文的根目录下,就必须通过ServletContext的getResource或getResourceAsStream定位。如: ServletContext context = ...; InputStream is = context.getResourceAsStream("/Image/ma.png");上述代码定位到的文件位置是:WebRoot Image ma.png如果你要在Java类中使用文件,则必须将“/Image/ma.png”放在类路径下,并通过Class.getResource或Class.getResourceAsStream定位,如: InputStream is = [YourClass].class.getResourceAsStream("/Image/ma.png");上述代码定位到的文件位置是:WebRoot WEB-INF classes Image ma.png关于资源定位问题,请参考我的博文:Java资源的定位方式http://blog.csdn.net/darxin/archive/2009/11/07/4782646.aspx 你忘了还有个WebRoot了?File imgFile=new File("WebRoot/Image/ma.png"); 这样是不行的,file取的是文件路径。就像你自己找文件一样,如果给一个/image/a.png这样的路径给你,让你去一台计算机上找,你从何找起类! jsp连接数据库出错 JS正则表达式存储的问题 session问题求助 高分求图书订购系统实例(jsp+javabean 杰表.web打印控件升级 The value for the useBean class attribute checkLogin is invalid! 这个是什么错误?分不够再加! 一个很简单的问题进来看下着急~~~~字符串分开再合并 在tomcat 4.1.12 中,如何进行数据连接池的配置? servlet中文显示问题 关于ResultSet的问题 如何判断标识用户(单台电脑) struts2问题
外部的资源就直接在页面上使用相对路径
时楼上说的很对,这里的路径得用文件在服务器上的绝对路径。
取绝对路径的方法网上查查就有了。
去掉/是从当前工程目录下开始找。
//就算在工程下也不行!
File imgFile = new File("Image/ma.png");
//class.getresources 怎么取当前工程路径!
获得当前服务器觉得路径,如:d:/tomcat6/webapps/testdemo
如果你的ma.png在工程image下那就是
getServlet().getServletContext().getRealPath("/")
+"Image/ma.png"
InputStream is = context.getResourceAsStream("/Image/ma.png");上述代码定位到的文件位置是:
WebRoot
Image
ma.png如果你要在Java类中使用文件,则必须将“/Image/ma.png”放在类路径下,并通过Class.getResource或Class.getResourceAsStream定位,如: InputStream is = [YourClass].class.getResourceAsStream("/Image/ma.png");上述代码定位到的文件位置是:
WebRoot
WEB-INF
classes
Image
ma.png关于资源定位问题,请参考我的博文:Java资源的定位方式
http://blog.csdn.net/darxin/archive/2009/11/07/4782646.aspx