0

我正在编写一个应该将文件发送到 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 方面简单地接受吗?

4

2 回答 2

2

我认为问题在这里:

InputStreamBody isb= new InputStreamBody(new ByteArrayInputStream(data), "file");

这是发送名为“文件”的数据,但随后

$_FILES['uploadedfile']['name']

正在尝试查找名为“uploadedfile”的文件。确保它们匹配。

于 2012-07-12T21:17:29.640 回答
1
iF YOU WANT TO UPLOAD .pdf FILE TO LOCAL SERVER THEN USE THIS SIMPLE METHOD, Lets we are doing code here under Button Click Event...

if (isset($_POST['submit']))
{

if ( ($_FILES["file"]["type"] =="application/pdf"))
 { 

 if (file_exists("C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]))

    echo " This File is already exists in folder";

else
{
  move_uploaded_file ($_FILES["file"]["tmp_name"],"C:/xampplite/htdocs/site/upload/" . $_FILES["file"]["name"]);      
  echo "File have been Stored in:-C:/xampplite/htdocs/site/upload/ "  . $_FILES["file"]["name"];

  }
}

}//end of click_event
于 2013-01-07T07:12:35.080 回答