vendredi 6 mars 2015

How to create and delete an empty file on a remote server in Java



How to create and delete an empty file on a remote server like http://ift.tt/1CP4ytv in Java for an Android app like input.lck Java Client side to create:



HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ift.tt/1CP4ytv");

File sourceFile = new File("input.lck");
myentity.addPart("image", new FileBody(sourceFile));

totalSize = myentity.getContentLength();
httppost.setEntity(myentity);


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://ift.tt/1aRC8Z2");
FileUtils.copyURLToFile(url, file);
file.delete()



Aucun commentaire:

Enregistrer un commentaire