转自博客:http://blog.csdn.net/l375852247/article/details/7999063

import java.net.MalformedURLException;
import java.net.URL; public class dd { /**
* @param args
*/
public static void main(String[] args) {
try {
URL url = new URL("file://ftp.yoyodyne.com/pub/files/foobar.txt?id=123456");
System.out.println("url.getFile()="+url.getFile());
System.out.println("url.getPath()="+url.getPath());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}

返回的结果:

url.getFile()=/pub/files/foobar.txt?id=123456
url.getPath()=/pub/files/foobar.txt

当将上面的

file://ftp.yoyodyne.com/pub/files/foobar.txt?id=123456 

改成

file://ftp.yoyodyne.com/pub/files/foobar.txt  

时:

返回的结果:

url.getFile()=/pub/files/foobar.txt
url.getPath()=/pub/files/foobar.txt

Java docs文档上说的:

The URL.getFile() javadocs say this:

Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.

They will be the same unless there is a query string, e.g. a ?somename=value&somethingelse=value2 in the URL.

我是一个新手之前一直不清楚是怎么回事,现在明白了!

URL的getFile()和getPath()方法的区别(转)的更多相关文章

  1. File和URL的getPath()方法区别

    java.io.File对象的getPath()方法返回文件的全路径名.如果是目录返回目录路径且结尾没有"\".如果是文件包含文件名. java.io.File对象的getName ...

  2. get方法与post方法的区别与js获取url参数的方式

    1.get方法与post方法的区别: 区别一:get重点在从服务器上获取资源,post重点在向服务器发送数据:区别二:get传输数据是通过URL请求,以field(字段)= value的形式,置于UR ...

  3. [BS-27] 创建NSURL的几个方法的区别

    创建NSURL的几个方法的区别     URL的基本格式 = 协议://主机地址/路径 URL和Path的区别 * URL:统一资源定位符,格式 “协议+主机名称+路径”   例如:[NSURL UR ...

  4. Cesium 中两种添加 model 方法的区别

    概述 Cesium 中包含两种添加 model 的方法,分别为: 通过 viewer.entities.add() 函数添加 通过 viewer.scene.primitives.add() 函数添加 ...

  5. HTTP协议中GET和POST方法的区别

    转载 通常的理解 w3schools关于这个问题的解答:HTTP 方法:GET 对比 POST 列出了一般的理解: 方法 GET POST 后退按钮/刷新 无害 数据会被重新提交(浏览器应该告知用户数 ...

  6. 转:GET和POST两种基本请求方法的区别

    原文地址:GET和POST两种基本请求方法的区别 原文如下: GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL ...

  7. django项目----函数和方法的区别

    一.函数和方法的区别 1.函数要手动传self,方法不用传 2.如果是一个函数,用类名去调用,如果是一个方法,用对象去调用 举例说明: class Foo(object): def __init__( ...

  8. python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别

    python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别 os.path.realpath 获取当前执行脚本的绝对路径. os.path.rea ...

  9. JS教程:window.location使用方法的区别

    介绍了window.location使用方法的区别. window.location.href=&http://www.jbxue.com/javascript/ldquo;url”:改变ur ...

随机推荐

  1. Ubuntu16.04 国内更新源

    在修改source.list之前要先备份 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 替换内容到source.list中 阿里云源: ...

  2. 设置WebApi里面命名空间参数

    在这种情况下: 如果没有特别处理,会报: 所以要像MVC中的控制器一下配置一个命名空间参数,webapi里面没有自带这个功能 代码: using System; using System.Collec ...

  3. POJ 1185 炮兵阵地(状压DP)题解

    思路:和上一篇思路一样,但是这里要求最大能排几个,这里要开三维,记录上次和上上次的状态,再一一判定,状态转移方程为 dp[i][j][k] = max(dp[i][j][k],dp[i - 1][k] ...

  4. 安装etcd集群

    kuberntes 系统使用 etcd 存储所有数据,是最重要的组件之一,注意 etcd集群只能有奇数个节点(1,3,5...),本文档使用3个节点做集群. 一.基础环境 软件包 etcd下载地址:h ...

  5. Asynchronous Programming Patterns

    Asynchronous Programming Patterns The .NET Framework provides three patterns for performing asynchro ...

  6. js模拟类的创建以及继承的四部曲

    <script> 1)创建父类 function Person(){ } Person.prototype.age = 18;//给父类添加属性 var p1 = new Person() ...

  7. [Pytorch]Pytorch加载预训练模型(转)

    转自:https://blog.csdn.net/Vivianyzw/article/details/81061765 东风的地方 1. 直接加载预训练模型 在训练的时候可能需要中断一下,然后继续训练 ...

  8. Codeforces Round #402 (Div. 2) A,B,C,D,E

    A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. 51nod 1040 最大公约数的和 欧拉函数

    1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 给出一个n,求1-n这n个数,同n的最大公约数 ...

  10. python 将类属性转为字典

    class dictObj(object): def __init__(self): self.x = 'red' self.y = 'Yellow' self.z = 'Green' def do_ ...