我在写一个文件上传练习时遇到一个奇怪的问题:

       String imaPath = request.getParameter("myfile");//获取上传的文件路径
       String writePath = request.getRealPath("image");//获取image文件夹路径

// System.out.println(imaPath);
// System.out.println(writePath);
     //  FileInputStream fis = new FileInputStream(imaPath);//这样写找不到正确路径 FileInputStream fis = new FileInputStream(request.getParameter("myfile"));
                                                    //这里用imaPath来代替为什么不行呢????
各位帮帮忙~~

解决方案 »

  1.   

    String imaPath = request.getParameter("myfile");//
    String realPath = request.getRealPath(imaPath);//FileInputStream fis = new FileInputStream(realPath); 
      

  2.   

    request.getSession().getServletContext().getRealPath("/image");
    获取你当前项目的image目录
      

  3.   

    因为你的路径只是一个虚拟目录
    譬如你传递过来的是:/images/test.jpg
    那么你使用request.getRealPath("/images/test.jpg");将会获得:D:\\tomcat安装目录\\webapps\\你的项目名称\\images\\test.jpg
    这样才是一个真实的文件目录地址
    使用new FileInputStream(这里需要真实的文件目录地址);才能找到    否则是会报错的     因为它不识别虚拟目录
      

  4.   

    这样可以用??
    如果你是一台机器充当服务器和客户机还可能行,你imaPath和writePath里存的都有是客户机里文件的路径啊,