Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android 4.03上,文件不使用http上传到服务器_Android - Fatal编程技术网

在Android 4.03上,文件不使用http上传到服务器

在Android 4.03上,文件不使用http上传到服务器,android,Android,我使用安卓4.03将一个文件上传到我的PHP服务器。我已经用一个HTML文件测试了我的PHP代码,效果很好。当我尝试使用Android时,我得到了200的HTTP响应。该文件未上载。这是我的密码 String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); baseDir = baseDir + "/270.jpg"; Log.d(TAG, "RegFile: " + base

我使用安卓4.03将一个文件上传到我的PHP服务器。我已经用一个HTML文件测试了我的PHP代码,效果很好。当我尝试使用Android时,我得到了200的HTTP响应。该文件未上载。这是我的密码

    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    baseDir = baseDir + "/270.jpg"; 
    Log.d(TAG, "RegFile: " + baseDir);
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;

    String pathToOurFile = baseDir;
    String urlServer = "http://192.168.15.5/upload_file.php";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    int serverResponseCode = connection.getResponseCode();
    String serverResponseMessage = connection.getResponseMessage();
    Log.d(TAG, "Server Response Code: " + serverResponseCode);
    Log.d(TAG, "Server Response Message: " + serverResponseMessage);
    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
        Log.d(TAG, "Server Error " + ex);
    }

我建议使用HttpClient上传文件。 这是一个.jar库,有关更多信息,请参见: