使用 HttpUrlConnection.getInputStream 和 FileUtils.copyInputStreamToFile 未下载 docx 和 xlsx 文件

docx and xlsx files not being downloaded using HttpUrlConnection.getInputStream and FileUtils.copyInputStreamToFile

我正在尝试通过 webdav 下载一些文件。我有一些代码似乎适用于除 Microsoft 文件之外的所有文件。 (例如,docx、xlsx)"work",我的意思是说我 运行 程序,我可以在我指定的保存位置找到文件。

代码如下:

    String[] folderPaths = path.split("/");
    String fileName = folderPaths[folderPaths.length - 1];

    String webdavURL = "https://" + WEB_HOST + "/webdav/";

    if(path.startsWith("/"))
            path = path.replaceFirst("/","");

    //System.out.println(webdavURL + folder + fileName);
    java.net.URL url = new java.net.URL(webdavURL + path);
    java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();

    //conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestMethod("GET");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestProperty("Authorization", "Basic " + new String(new org.apache.commons.codec.binary.Base64().encode("un:pw".getBytes())));
    conn.setRequestProperty("User-Agent","curl/7.37.0");
    conn.setRequestProperty("Host", WEB_HOST);
    conn.setRequestProperty("Accept", "*/*");
    //conn.setRequestProperty("Content-Length", String.valueOf(fileContents.length));
    //conn.setRequestProperty("Expect", "100-continue");
    org.apache.commons.io.FileUtils.copyInputStreamToFile(conn.getInputStream(), file);

    //org.apache.commons.io.FileUtils.writeByteArrayToFile(file, org.apache.commons.io.IOUtils.toByteArray(conn.getInputStream()));

    Integer returnCode = conn.getResponseCode();

    java.io.InputStream errorStream = conn.getErrorStream();

    if(errorStream != null)
        System.out.println(org.apache.commons.io.IOUtils.toString(conn.getErrorStream()));

    java.util.Map<String, java.util.List<String>> map = conn.getHeaderFields();
    for (java.util.Map.Entry<String, java.util.List<String>> entry : map.entrySet()) {
        System.out.println("Key : " + entry.getKey() + 
             " ,Value : " + entry.getValue());
    }

    conn.disconnect();

    return returnCode;

如您所见,我正在打印一些行以进行调试。

以下是成功下载的响应 headers:

Key : null ,Value : [HTTP/1.1 200 OK]
Key : ETag ,Value : ["179614-BA45C8F60456A672E003A875E469D0EB"]
Key : Content-Length ,Value : [845941]
Key : Expires ,Value : [-1]
Key : Last-Modified ,Value : [Fri, 04 Apr 2014 14:13:54 GMT]
Key : Set-Cookie ,Value : [stuff]
Key : X-Powered-By ,Value : [ARR/2.5]
Key : Server ,Value : [Microsoft-IIS/7.5]
Key : Cache-Control ,Value : [private]
Key : Pragma ,Value : [private]
Key : Date ,Value : [Wed, 23 Dec 2015 19:01:39 GMT]
Key : P3P ,Value : [CP="CAO PSA OUR"]
Key : Content-Type ,Value : [image/jpeg]
Key : Accept-Ranges ,Value : [bytes]

这里是对 xlsx 文件的响应 headers:

Key : null ,Value : [HTTP/1.1 200 OK]
Key : ETag ,Value : ["205147-70BDF9AF17A2F13756A21AE50EB88DFF"]
Key : Content-Length ,Value : [48002]
Key : Expires ,Value : [-1]
Key : Last-Modified ,Value : [Fri, 29 Aug 2014 20:27:51 GMT]
Key : Set-Cookie ,Value : [stuff]
Key : X-Powered-By ,Value : [ARR/2.5]
Key : Server ,Value : [Microsoft-IIS/7.5]
Key : Cache-Control ,Value : [private]
Key : Pragma ,Value : [private]
Key : Date ,Value : [Wed, 23 Dec 2015 19:01:38 GMT]
Key : P3P ,Value : [CP="CAO PSA OUR"]
Key : Content-Type ,Value : [application/x-www-form-urlencoded]
Key : Accept-Ranges ,Value : [bytes]

我真的不知道这里可能出了什么问题。我从服务器收到 200 响应,并且没有报告任何错误或异常。

唯一让我印象深刻的是,当尝试下载 xlsx 文件时,Content-Type 是 application/x-www-form-urlencoded 而与其他任何东西相比,content-type 实际上列出了它的文件类型。我没想到会有什么不同。

如有任何想法,我们将不胜感激!

conn.setDoInputconn.setDoOutput。评论那些使它起作用。我真的不知道他们做了什么;我刚刚从以前的项目中复制了这段代码。