new EasySSLSocketFactory(), 443));\r
}\r
\r
+ /**\r
+ * Downloads a file in remoteFilepath to the local targetPath.\r
+ * \r
+ * @param remoteFilepath Path to the file in the remote server, URL DECODED. \r
+ * @param targetPath Local path to save the downloaded file.\r
+ * @return 'True' when the file is successfully downloaded.\r
+ */\r
public boolean downloadFile(String remoteFilepath, File targetPath) {\r
- GetMethod get = new GetMethod(mUri.toString() + remoteFilepath);\r
+ boolean ret = false;\r
+ GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));\r
HttpMethodParams params = get.getParams();\r
params.setSoTimeout(0); // that means "infinite timeout"; it's the default value, but let's make it explicit\r
get.setParams(params);\r
try {\r
int status = executeMethod(get);\r
Log.e(TAG, "status return: " + status);\r
- if (status != HttpStatus.SC_OK) {\r
- return false;\r
- }\r
- BufferedInputStream bis = new BufferedInputStream(\r
- get.getResponseBodyAsStream());\r
- FileOutputStream fos = new FileOutputStream(targetPath);\r
-\r
- byte[] bytes = new byte[4096];\r
- int readResult;\r
- while ((readResult = bis.read(bytes)) != -1) {\r
- if (mDataTransferListener != null)\r
- mDataTransferListener.transferProgress(readResult);\r
- fos.write(bytes, 0, readResult);\r
- }\r
+ if (status == HttpStatus.SC_OK) {\r
+ targetPath.createNewFile();\r
+ BufferedInputStream bis = new BufferedInputStream(\r
+ get.getResponseBodyAsStream());\r
+ FileOutputStream fos = new FileOutputStream(targetPath);\r
\r
- } catch (IOException e) {\r
+ byte[] bytes = new byte[4096];\r
+ int readResult;\r
+ while ((readResult = bis.read(bytes)) != -1) {\r
+ if (mDataTransferListener != null)\r
+ mDataTransferListener.transferProgress(readResult);\r
+ fos.write(bytes, 0, readResult);\r
+ }\r
+ \r
+ }\r
+ ret = true;\r
+ } catch (Throwable e) {\r
e.printStackTrace();\r
- return false;\r
+ targetPath.delete();\r
}\r
- return true;\r
+ \r
+ return ret;\r
}\r
\r
/**\r
* Deletes a remote file via webdav\r
- * @param remoteFilePath\r
+ * @param remoteFilePath Remote file path of the file to delete, in URL DECODED format.\r
* @return\r
*/\r
public boolean deleteFile(String remoteFilePath){\r
- DavMethod delete = new DeleteMethod(mUri.toString() + remoteFilePath);\r
+ DavMethod delete = new DeleteMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilePath));\r
try {\r
executeMethod(delete);\r
} catch (Throwable e) {\r
mDataTransferListener = listener;\r
}\r
\r
+ /**\r
+ * Creates or update a file in the remote server with the contents of a local file.\r
+ * \r
+ * \r
+ * @param localFile Path to the local file to upload.\r
+ * @param remoteTarget Remote path to the file to create or update, URL DECODED\r
+ * @param contentType MIME type of the file.\r
+ * @return 'True' then the upload was successfully completed\r
+ */\r
public boolean putFile(String localFile, String remoteTarget,\r
String contentType) {\r
boolean result = true;\r
FileRequestEntity entity = new FileRequestEntity(f, contentType);\r
entity.setOnDatatransferProgressListener(mDataTransferListener);\r
Log.e("ASD", f.exists() + " " + entity.getContentLength());\r
- PutMethod put = new PutMethod(mUri.toString() + remoteTarget);\r
+ PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget));\r
HttpMethodParams params = put.getParams();\r
params.setSoTimeout(0); // that means "infinite timeout"; it's the default value, but let's make it explicit\r
put.setParams(params);\r
return returnCode;\r
}\r
\r
+ /**\r
+ * Creates a remote directory with the received path.\r
+ * \r
+ * @param path Path of the directory to create, URL DECODED\r
+ * @return 'True' when the directory is successfully created\r
+ */\r
public boolean createDirectory(String path) {\r
try {\r
- MkColMethod mkcol = new MkColMethod(mUri.toString() + path);\r
+ MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path));\r
int status = executeMethod(mkcol);\r
Log.d(TAG, "Status returned " + status);\r
Log.d(TAG, "uri: " + mkcol.getURI().toString());\r