android + php无法上传图片?

时间:2012-06-28 15:40:42

标签: php android phpmyadmin

我正在使用localhost for php。

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class ImageUpload {
    HttpURLConnection _httpurlconnection;
    DataOutputStream _dos;
    DataInputStream _dis;
    String _serverurl="http://192.168.1.11/project/imageupload.php";
    String _lineend= "\r\n";
    String _twohyphens= "--";
    String _boundry="*****";
    int _bytesread, _bytesavailable, _buffersize;
    byte[] _buffer;
    int _maxbuffersize = 1 * 1024 * 1024;

    public void uploadImage(String _filepath) {
        try {
            FileInputStream _fis = new FileInputStream(new File(_filepath));
            URL _url = new URL(_serverurl);
            _httpurlconnection = (HttpURLConnection) _url.openConnection();
            _httpurlconnection.setDoInput(true);
            _httpurlconnection.setDoOutput(true);
            _httpurlconnection.setUseCaches(false);
            _httpurlconnection.setRequestMethod("POST");
            _httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
            _httpurlconnection.setRequestProperty("Connection-Type",
                    "multipart/form-data;boundry=" + _boundry);
            _dos = new DataOutputStream(_httpurlconnection.getOutputStream());
            _dos.writeBytes(_twohyphens + _boundry + _lineend);
            _dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                + _filepath + "\"" + _lineend);

            _dos.writeBytes(_lineend);

            _bytesavailable = _fis.available();
            _buffersize = Math.min(_bytesavailable, _maxbuffersize);
            _buffer = new byte[_buffersize];
            _bytesread = _fis.read(_buffer, 0, _buffersize);


            while (_bytesread > 0) {


                _dos.write(_buffer, 0, _buffersize);
                _bytesavailable = _fis.available();
                _buffersize = Math.min(_bytesavailable, _maxbuffersize);
                _bytesread = _fis.read(_buffer, 0, _buffersize);

            }
            _dos.writeBytes(_lineend);
            _dos.writeBytes(_twohyphens + _boundry + _twohyphens + _lineend);

            int responsecode = _httpurlconnection.getResponseCode();
            String responsemessage = _httpurlconnection.getResponseMessage();
            _fis.close();
            _dos.flush();
            _dos.close();

            BufferedReader br=new BufferedReader(new InputStreamReader(_httpurlconnection.getInputStream()));
            String line;
            while((line=br.readLine())!=null)
            {
                Log.i("----------------",">>"+line);
            }
            br.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

这是我的PHP代码。

<?php

$target_path  = "/var/www/project";
$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!";
}
?>

但是我收到通知:未定义的索引:第5行的上传文件。 任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

看起来服务器上不存在您为服务器提供的_filepath文件夹。

答案 1 :(得分:1)

未定义的索引警告显示无论出于何种原因,请求中都不存在文件uploadedfile

这个问题有一些常见的警告,最初与php.ini文件中的配置指令有关。

  1. 您的上传尺寸上限是否足够高?它们位于max_post_sizeupload_max_filesize指令中。
  2. 对于大文件,上传的超时时间是否足够长? max_execution_time
  3. 上传的临时文件的路径是否配置正确且可写?
  4. 缩小它们,故障通常在于客户端代码,无论是html / js还是网页浏览器中表单中遗忘的enctype,还是像您一样的移动设备上的本机代码。< / p>

    我建议查看一个安卓库来帮助你构建请求对象,因为multipart类型请求可能会很棘手。有一些方法可以使用org.apache库来执行此操作,而无需像尝试那样编写请求的实际文本。

    http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/