How to create and delete an empty file on a remote server like http://example.com/myfolder in Java for an Android app like input.lck Java Client side to create:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/myfolder");
File sourceFile = new File("input.lck");
myentity.addPart("image", new FileBody(sourceFile));
httppost.setEntity(myentity);
HttpResponse response = httpclient.execute(httpPost);
Server side PHP code:
<?php
echo $_FILES['image']['name'] . '<br/>';
$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['image']['name']);
try {
//throw exception if can't move the file
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
throw new Exception('Could not move file');
}
echo "The file " . basename($_FILES['image']['name']) .
" has been uploaded";
} catch (Exception $e) {
die('File did not upload: ' . $e->getMessage());
}
?>
Code to delete:
File file = new File("input.lck");
URL url = new URL("http://example.com/myfolder/input.lck");
FileUtils.copyURLToFile(url, file);
file.delete()
create input.lck with HttpPut:
HttpPut httpPut = new HttpPut("http://10.0.2.2/media/fileUpload.php");
File sourceFile = new File("input.lck");
entity.addPart("image", new FileBody(sourceFile));
httpPut.setEntity(entity);
HttpResponse response = httpclient.execute(httpPut);
HttpEntity r_entity = response.getEntity();
which gives FileNotFound error on PHP
Delelete input.lck with HttpDelete:
HttpDelete httpDelete = new HttpDelete("http://10.0.2.2/media/uploads/input.lck");
HttpResponse response = httpclient.execute(httpDelete);
HttpEntity r_entity = response.getEntity();
which gives 405 response code