我正在编写一个应该将文件发送到 PHP 服务器的应用程序。这是我的代码:
InputStream is = new FileInputStream(file);
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://majkelsoftgames.cba.pl/ser/server.php");
byte[] data = IOUtils.toByteArray(is);
InputStreamBody isb= new InputStreamBody(new ByteArrayInputStream(data), "file");
MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("file", isb);
postRequest.setEntity(multipartContent);
HttpResponse response = httpClient.execute(postRequest);
我的问题是我真的没有 PHP 方面的经验,而且我不知道如何在 PHP 端获取这个文件。我找到了一些代码:
<?php
$target_path = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
}
else {
echo "There was an error uploading the file, please try again!";
}
?>
但它不起作用。有人可以解释一下我怎样才能在 PHP 方面简单地接受吗?