Java 未使用HttpUrlConnection.getInputStream和FileUtils.copyInputStreamToFile下载docx和xlsx文件
我正试图通过webdav下载一些文件。我有一些代码似乎适用于除Microsoft文件以外的所有文件。(例如,docx,xlsx)通过“work”,我的意思是说我运行了这个程序,我可以在我指示保存它的地方找到文件 代码如下:Java 未使用HttpUrlConnection.getInputStream和FileUtils.copyInputStreamToFile下载docx和xlsx文件,java,excel,ms-word,httpurlconnection,fileutils,Java,Excel,Ms Word,Httpurlconnection,Fileutils,我正试图通过webdav下载一些文件。我有一些代码似乎适用于除Microsoft文件以外的所有文件。(例如,docx,xlsx)通过“work”,我的意思是说我运行了这个程序,我可以在我指示保存它的地方找到文件 代码如下: String[] folderPaths = path.split("/"); String fileName = folderPaths[folderPaths.length - 1]; String webdavURL = "https://"
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;
以下是xlsx文件的响应头:
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文件时,内容类型是application/x-www-form-urlencoded
,而对于其他内容,内容类型实际上列出了它所属的文件类型。我没想到那会有什么不同
任何想法都将不胜感激 是
conn.setDoInput
和conn.setDoOutput
。对这些问题的评论使它起了作用。我真的不知道他们做了什么;我刚从以前的项目中复制了这段代码
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]