我的上传文件到了tomcat下。我知道肯定是上传路径不对,不知道在哪里改。希望有人能帮忙指点下。
代码如下:
InputStream is = new FileInputStream(myFile);//获取上传文件
String path = ServletActionContext.getRequest().getRealPath("/file");
 String tempname = path+"\\"myFileFileName;
OutputStream os = new FileOutputStream(tempname);//IO操作文件复制到指定目录
while((len = is.read(b))!=-1){
os.write(b,0,len);
                        }
os.close();
        is.close();

解决方案 »

  1.   

     ServletActionContext.getRequest().getRealPath这个是取得tomcat配置的路径,你试试这个呢ServletActionContext.getServletContext().getRealPath
      

  2.   

    要想把文件上传到工程目录下,工程就不能部署到tomcat中,
    首先要把 /Tomcat/webapps/ 这个目录清空,删掉该目录的所有内容,
    然后把  /Tomcat/conf/web.xml  的监听器改为 true<servlet>
            <servlet-name>default</servlet-name>
            <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
            <init-param>
                <param-name>debug</param-name>
                <param-value>0</param-value>
            </init-param>
            <init-param>
                <param-name>listings</param-name>
                <param-value>true</param-value><!--把false改为true-->
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>在 /Tomcat/conf/server.xml 中配置工程的虚拟路径<Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true"
                xmlValidation="false" xmlNamespaceAware="false">
           
    <!----><Context path="/deco" docBase="E:\lft\deco\WebRoot"></Context>
    <!--<Context path="/ser" docBase="D:\project\myProject\editor\WebRoot"></Context>-->
    <!--这里可以配置多个工程的虚拟路径, 不想运行的工程就注释掉吧-->
          </Host>
    这样配置后,
    String path = ServletActionContext.getRequest().getRealPath("/");
    /**
    得到的就是你工程的WebRoot根目录, 文件上传时就传到了工程目录下,
    request.getRealPath(String arg) 这个方法已经过时,
    还是用 request.getSession().getServletContext().getRealPath(String arg) 这个方法吧
    String tempname = path+"\\"myFileFileName; 这里需要注意
    不要用双斜杠"\\",尽量用反斜杠"/"
    因为双斜杠在linux系统中不能被识别
    */
      

  3.   

    我知道了,因为自己的工程师部署到tomcat下的。在访问的时候获得上下文路径例如:http://localhost:8080/工程名称/file/(上传的文件)   这样如果能显示就说明已经上传成功了     
    在调用的时候只要获得上下文路径然后拼接文件名就可以了。