I'm trying to download a some files via webdav. I have some code which seems to work for all files except for Microsoft files. (e.g., docx, xlsx) By "work", I mean to say that I run the program, and I can find the file in the place that I directed it to be saved.
Here is the code:
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;
As you can see, I am printing some lines for debugging purposes.
Here are the response headers for a successful download:
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]
And here are the response headers for an xlsx file:
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]
I really have no idea what might be going wrong here. I get a 200 response from the server, and there are no reported errors or exceptions.
The only thing that stands out to me is that, when trying to download an xlsx file, the Content-Type is application/x-www-form-urlencoded
while with anything else, the content-type actually lists the type of file that it is. I would not have thought that that would make a difference.
Any ideas at all would be greatly appreciated!
it was conn.setDoInput
and conn.setDoOutput
. Commenting those out made it work. I didn't really know what they did; I just copied this code from a previous project.