a-schild/nextcloud-java-api

Downloaded file size is always 0 KB

Vaibhav1587 opened this issue · 4 comments

I'm able to get all the files from server.
But while downloading getting following exception,

Exception in thread "pool-1-thread-8" org.aarboard.nextcloud.api.exception.NextcloudApiException: com.github.sardine.impl.SardineException: Unexpected response (500 Internal Server Error)
	at org.aarboard.nextcloud.api.webdav.Files.fileExists(Files.java:47)
	at org.aarboard.nextcloud.api.webdav.Files.downloadFile(Files.java:111)
	at com.netand.cloud.utility.DownloadFileTask.downloadFile(DownloadFileTask.java:28)
	at com.netand.cloud.utility.DownloadFileTask.run(DownloadFileTask.java:21)
	at com.netand.cloud.SyncData.lambda$0(SyncData.java:45)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: com.github.sardine.impl.SardineException: Unexpected response (500 Internal Server Error)

Also tried to download the single file just to check.

Server location : /Photos/Nut.jpg

Console output for the same is as follow,

Aug 02, 2018 3:32:15 AM com.github.sardine.impl.io.HttpMethodReleaseInputStream close
WARNING: Abort connection for response HttpResponseProxy{HTTP/1.1 200 OK [Date: Wed, 01 Aug 2018 22:02:14 GMT, Server: Apache/2.4.33 (cPanel) OpenSSL/1.0.2o mod_bwlimited/1.4 Phusion_Passenger/5.1.12, X-Powered-By: PHP/5.6.36, Expires: Thu, 19 Nov 1981 08:52:00 GMT, Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, Pragma: no-cache, X-Frame-Options: SAMEORIGIN, Content-Security-Policy: default-src 'none';, Content-Disposition: attachment; filename*=UTF-8''Nut.jpg; filename="Nut.jpg", OC-ETag: "a5368504149f4bd326e9cd248a891479", Set-Cookie: oc_sessionPassphrase=sCLZMQyWBFBPLT24YTaUTisb8REMnV%2FF10cHagqvLkm%2FUEXDozRAsqSiqP7kPpQ6yZsrC%2F8FQftKFjwbZu2i1AZAD5DjiKGjY3MO85es92GDPeX79o8yoGinLgrHjeyt; path=/8cloud8; secure; httponly, Set-Cookie: nc_sameSiteCookielax=true; path=/xxxxxxx; httponly;secure; expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=lax, Set-Cookie: nc_sameSiteCookiestrict=true; path=/xxxxxxx; httponly;secure; expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=strict, Set-Cookie: ochjaxalez7q=l22617h9n2g47ssg3dnbi1n4d1; path=/xxxxxxx; secure; HttpOnly, Set-Cookie: cookie_test=test; expires=Wed, 01-Aug-2018 23:02:15 GMT; Max-Age=3600, Last-Modified: Wed, 28 Mar 2018 17:23:44 GMT, ETag: "a5368504149f4bd326e9cd248a891479", Content-Length: 955026, X-Content-Type-Options: nosniff, X-XSS-Protection: 1; mode=block, X-Robots-Tag: none, X-Download-Options: noopen, X-Permitted-Cross-Domain-Policies: none, Keep-Alive: timeout=3, max=29, Connection: Keep-Alive, Content-Type: image/jpeg] ResponseEntityProxy{[Content-Type: image/jpeg,Content-Length: 955026,Chunked: false]}}

Can you please try with another file?
Can you see what the nextcloud server is throwing for an exception?

A error 500 is a server side error, which usually indicates that something has gone wrong on serverside

I tried but won't work.

So, I did some changes in code it works but fail to download the bigger size file. like above 40MB cant download.

Your code : in Files.java

			in = sardine.get(path);
			byte[] buffer = new byte[in.available()];
			in.read(buffer);
			File targetFile = new File(downloadirpath);
			OutputStream outStream = new FileOutputStream(targetFile);
			outStream.write(buffer);
			status = true;

Updated code:

		InputStream in = sardine.get(path);
		byte[] buffer = new byte[1024];//new byte[in.available()];
		int bytesRead;
		File targetFile = new File(downloadirpath);
		OutputStream outStream = new FileOutputStream(targetFile);
                while((bytesRead = in.read(buffer)) !=-1){
                        outStream.write(buffer, 0, bytesRead);
                }
                outStream.flush();
                outStream.close();
	        status = true;

Thanks for fixing it.
Your code is now included in the 11.0.2 release