0

我正在使用 localhost 进行 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 行的上传文件。有人可以帮我解决这个问题吗?

4

2 回答 2

1

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

于 2012-06-28T15:45:45.647 回答
1

未定义索引警告表明,无论出于何种原因,uploadedfile请求中都不存在该文件。

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

  1. 您的上传大小上限是否设置得足够高?这些在max_post_sizeandupload_max_filesize指令中。
  2. 对于大文件来说,上传的超时时间是否足够长?max_execution_time
  3. 上传的临时文件的路径是否配置正确且可写?

缩小范围,故障通常出在客户端代码上,可能只是 html/js 和enctypeWeb 浏览器中被遗忘的表单,或者像您拥有的移动设备上的本机代码。

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

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

于 2012-06-28T15:48:02.947 回答