如何在服务器上获取文件的实际名称?

时间:2015-04-02 14:46:45

标签: java

我试图从以下网址获取文件名:

http://www.example.com/to/the/path/File.txt

在java by:

public String getFileName(URL url) {    
   String filename = url.getFile();    
   return filename.substring(filename.lastIndexOf('/')+1); 
}

但是如何从以下位置获取文件的实际文件名:

https://docs.google.com/uc?export=download&id=0BwScoO2FIDnQQlF2cHVLbHBjQzg
(它是我的Google云端硬盘上的公共共享文件。)

我经常看到,当我在网络浏览器的地址栏中放置这样的网址时,它能够识别文件的名称并将其保存为实际的文件名。在java中可以吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

好吧,简单地说,你需要调查究竟究竟是什么。获取CURL以查看正在发生的事情:

curl -D - -o /dev/null -L "https://docs.google.com/uc?export=download&id=0BwScoO2FIDnQQlF2cHVLbHBjQzg"

会给你类似的东西:

    Content-Type: text/html; charset=UTF-8
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    Pragma: no-cache
    Expires: Fri, 01 Jan 1990 00:00:00 GMT
    Date: Fri, 03 Apr 2015 17:42:47 GMT
    Location: https://doc-0c-8k-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/m2vr72uv54vrhd59tvsm5ellda89n0s6/1428076800000/17869486772410674836/*/0BwScoO2FIDnQQlF2cHVLbHBjQzg?e=download
    Set-Cookie: NID=67=fxFK6wjqCslAMuLajOnHKWYlkVAJDG6gMzoQnsDZI2wkd5QlP7meP8tDQJEQk9veY0UUOtSZWAjM_JbHAOMcJavkHU2B6-VDUo3jRpBQSm0moWVmN689L-I1vKgGJoVK;Domain=.google.com;Path=/;Expires=Sat, 03-Oct-2015 17:42:47 GMT;HttpOnly
    P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
    X-Content-Type-Options: nosniff
    X-Frame-Options: SAMEORIGIN
    X-XSS-Protection: 1; mode=block
    Server: GSE
    Alternate-Protocol: 443:quic,p=0.5
    Accept-Ranges: none
    Vary: Accept-Encoding
    Transfer-Encoding: chunked

    HTTP/1.1 200 OK
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: false
    Access-Control-Allow-Headers: Accept, Accept-Language, Authorization, Cache-Control, Content-Disposition, Content-Encoding, Content-Language, Content-Length, Content-MD5, Content-Range, Content-Type, Date, GData-Version, Host, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, Origin, OriginToken, Pragma, Range, Slug, Transfer-Encoding, X-ClientDetails, X-GData-Client, X-GData-Key, X-Goog-AuthUser, X-Goog-PageId, X-Goog-Encode-Response-If-Executable, X-Goog-Correlation-Id, X-Goog-Request-Info, X-Goog-Experiments, x-goog-iam-role, x-goog-iam-authorization-token, X-Goog-Spatula, X-Goog-Upload-Command, X-Goog-Upload-Content-Disposition, X-Goog-Upload-Content-Length, X-Goog-Upload-Content-Type, X-Goog-Upload-File-Name, X-Goog-Upload-Offset, X-Goog-Upload-Protocol, X-Goog-Visitor-Id, X-HTTP-Method-Override, X-JavaScript-User-Agent, X-Pan-Versionid, X-Origin, X-Referer, X-Upload-Content-Length, X-Upload-Content-Type, X-Use-HTTP-Status-Code-Override, X-YouTube-VVT, X-YouTube-Page-CL, X-YouTube-Page-Timestamp
    Access-Control-Allow-Methods: GET,OPTIONS
    Content-Type: application/zip
    Content-Disposition: attachment;filename="All.zip";filename*=UTF-8''All.zip
    Date: Fri, 03 Apr 2015 17:42:47 GMT
    Expires: Fri, 03 Apr 2015 17:42:47 GMT
    Cache-Control: private, max-age=0
    X-Goog-Hash: crc32c=yKvC2Q==
    Transfer-Encoding: chunked
    Server: UploadServer ("Built on Mar 27 2015 13:04:19 (1427486659)")
    Alternate-Protocol: 443:quic,p=0.5

在这里,您可以看到:

所以现在你想自动化这个,而且this link会帮助你。

快乐学习!

答案 1 :(得分:0)

您可以创建Object of File类并调用getName()方法来获取文件的绝对名称。

    File file = new File("http://www.example.com/to/the/path/File.txt");
    System.out.println(file.getName());

如果您没有文件的绝对路径,并且Google文档路径不是绝对文件路径,则无法获取文件名。