zoukankan      html  css  js  c++  java
  • "/FileUpLoad"文件的说明

     String ContextPath = request.getContextPath();这个获取的是上下文路径,一般就是项目名字,如果上下文路径为空,则这个输出也为空。

    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + ContextPath + "/";
    System.out.println("basePath:"+basePath);

    输出的效果是:basePath:http://localhost:8084/calls/

     request.getScheme() 获取到 http

    request.getServerName() 获取到localhost,就是IP地址

    request.getServerPort() 获取到 8084端口号

    String path = request.getSession().getServletContext().getRealPath("/");
    System.out.println("path:"+path);

    path:C:UsersAdministratorDocumentsNetBeansProjectsWebApplication11uildweb

    这个web文件夹下面就是/WEB-INF/之类的文件夹了

    request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了。getRealPath("/") 获取实际路径,“/”指代项目根目录,所以代码返回的是项目在容器中的实际发布运行的根路径如:I:workspace.metadata.pluginsorg.eclipse.wst.server.core mp1wtpwebappsUMPWeb_20131230

    得到的就是你tomcat下webapps下的项目根路径

    那么请问一下如果我往这个路径存文件是把文件存到了服务器上了吗?

    是的。

    request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了。getRealPath("/") 获取实际路径,“/”指代项目根目录,所以代码返回的是项目在容器中的实际发布运行的根路径
    
    
    在工程的WebContent下新建一个upload的目录
    // 文件保存路径
    String filePath = realPath + "upload/"+ file.getOriginalFilename();
  • 相关阅读:
    .NET程序默认启动线程数
    TPL中Task执行的内联性线程重入
    Unity容器中的对象生存期管理
    C# 异步 TCP 服务器完整实现
    WPF中多源控制Button的状态
    C# 对 TCP 客户端的状态封装
    WPF异步MVVM等待窗体
    C#实现UDP分包组包
    C#实现RTP数据包传输
    PHP 传引用调用
  • 原文地址:https://www.cnblogs.com/itchenfirst/p/7283976.html
Copyright © 2011-2022 走看看