从Android java应用程序向Codeigniter上传图片

从Android java应用程序向Codeigniter上传图片,java,php,android,codeigniter,post,Java,Php,Android,Codeigniter,Post,我和我的朋友在将图片从android应用程序上传到php中的Codeigniter框架时遇到了问题。问题出现了(我们认为),因为它不是图像文件,因此不能用于图像操作 一个重要的注意事项是,当使用html页面进行测试时,图像上传可以正常工作。所有其他数据(如电子邮件)都在android应用程序中提供并工作。唯一不起作用的是文件上传。但它确实可以与另一个php文件一起工作,但我的问题是如何将最后一个可以工作的php脚本转换为codeigniter?我想使用这个框架,但到目前为止我们还不能 以下是上传

我和我的朋友在将图片从android应用程序上传到php中的Codeigniter框架时遇到了问题。问题出现了(我们认为),因为它不是图像文件,因此不能用于图像操作

一个重要的注意事项是,当使用html页面进行测试时,图像上传可以正常工作。所有其他数据(如电子邮件)都在android应用程序中提供并工作。唯一不起作用的是文件上传。但它确实可以与另一个php文件一起工作,但我的问题是如何将最后一个可以工作的php脚本转换为codeigniter?我想使用这个框架,但到目前为止我们还不能

以下是上传的Java代码->

HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://url.com/controller-name");

try
{
  CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new ProgressListener()
  {
    @Override
    public void transferred(long num)
    {
      publishProgress((int) ((num / (float) totalSize) * 100));
    }
  });

  // We use FileBody to transfer an image
  multipartContent.addPart("userfile", new FileBody(new File(filename)));
  totalSize = multipartContent.getContentLength();
  multipartContent.addPart("email", new StringBody("email@email.com"));
  multipartContent.addPart("submit", new StringBody("upload"));

  // Send it
  httpPost.setEntity(multipartContent);
  HttpResponse response = httpClient.execute(httpPost, httpContext);
  String serverResponse = EntityUtils.toString(response.getEntity());

  Log.i("SERVER", "Response: " + response.toString());
  return serverResponse;
}

catch (Exception e)
{
  System.out.println(e);
}
用于上载的codeigniter方法:

function do_upload() {
    $image_name = time() . $this->get_random_string();
    $config = array(
        'file_name' => $image_name,
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path

    );
    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();
    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs',
        'maintain_ratio' => false,
        'width' => 300,
        'height' => 300
    );

    $this->load->library('image_lib', $config);
    $this->image_lib->resize();
    echo $this->image_lib->display_errors();

    $image_name = $image_name . $image_data['file_ext'];
    //Insert into the database
    $data = array(
        'image_name' => $image_name,
        'upload_email' => $this->input->post('email'),
        'ip' => $this->input->ip_address()
    );

    // Insert the new data of the image!
    $insert = $this->db->insert('table', $data);
    $short_url = $this->alphaID($this->db->insert_id());

    return $short_url;

}
可以使用的旧php脚本,请注意,注释后的代码无法使用。因为它可能会产生与CI相同的效果

<?php
/*if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "application/octet-stream")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{*/
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {   
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

        if (file_exists("images/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],
                "images/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "images/" . $_FILES["file"]["name"];
        }
    }
/*}
else
{
    echo "Invalid file";
}*/
?> 


请帮助我们修改CI函数或java代码,以便我们可以上传我们喜爱的图像。谢谢你的建议和更好的智慧

答案在java代码中。我需要向文件体添加更多的参数

  multipartContent.addPart("userfile", new FileBody(new File(filename), filename, "image/jpeg", "utf-8"));
这是完美的。
您可以在这里找到有关filebody构造函数的信息:

答案在java代码中。我需要向文件体添加更多的参数

  multipartContent.addPart("userfile", new FileBody(new File(filename), filename, "image/jpeg", "utf-8"));
这是完美的。
有关可在此处找到的filebody构造函数的信息:

如果添加echo$this->upload->display_errors();,应用程序将输出“未选择任何文件”,就好像它没有任何文件开始一样!:(稍微更改了java代码,得到了以下结果:如果添加echo$this->upload->display_errors();应用程序将输出“未选择任何文件”,就好像它没有任何文件可以开始一样!:(稍微更改了java代码,得到了以下结果: