日期:2014-05-17  浏览次数:20700 次

头痛致死!慌张致死啊,图片路径的问题!!!
我有一个查看图片的功能,图片的名称放在数据库里。我在本地测试没有问题,项目放到服务器上就找不到图片了。。。上传的图片竟然跑到

我用的是String path = request.getContextPath(); 
我去,这个和String path = request.getSession().getServletContext().getRealPath("/");有什么区别
还有一个String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath(); +"/";

这三个是啥子意思哟?????

我存图片的时候用的是String path = request.getSession().getServletContext().getRealPath("/");

取图片的时候用的是String path = request.getContextPath(); 

在本地没有出现问题,到服务器上就没了,据项目经理说上传时创建的文件夹竟然在项目外,可他项目怎么跑到webapp下?不熟那些哥不懂啊。。。。
大神来救我吧。

------解决方案--------------------
。。。。。。。。
在网页上访问只能用
String path = request.getContextPath();

String path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath(); +"/";

保存文件时用

String path = request.getSession().getServletContext().getRealPath("");

注意不要有 /

------解决方案--------------------
不知道是不是和名称的大小写有关 
发布后的文件比较容易出现这样的问题
------解决方案--------------------
Java code

public class TestServlet  extends HttpServlet{

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String s1 = request.getSession().getServletContext().getRealPath("/");
        System.out.println(s1);
        // F:\MyWorkSpaces\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\JavaWeb\
        
        String s2 = request.getContextPath();
        System.out.println(s2);
        // /JavaWeb
        
        String s3 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
        System.out.println(s3);
        // http://localhost:8081/JavaWeb/
        
    }
}

------解决方案--------------------
${pageContextl.request.contextPath} 得到webRoot 的绝对根路径