code,从Android的视频上传到PHP视频、code、Android、PHP

2023-09-04 10:39:38 作者:为你而来

我想从Android的code视频上传到我的PHP的Web服务器code,但无法获得成功。我指以下链接执行上传任务,但我得到在我的Andr​​oid code以下的反应,但不能找到PHP服务器上的任意文件。

Android的响应

  DEBUG /服务器code(29484):200
DEBUG / serverResponseMessage(29484):确定
 

我已经检查了很多东西像的php.ini 文件设定值。 虽然我可以从Android的code图像上传到服务器becasue从Android的我送一个64位的EN codeD 的ByteArray 和therer是一个现成的功能PHP可以从连接codeD 的ByteArray 秒。

制作图片

但是,code也不能正常工作的情况下比其他图像的任何其他类型的有文件的。

请指引我如果你们以前做过类似的事情。

我使用PHP code:

 < PHP

    $ target_path =./upload/;

    $ target_path = $ target_path。基本名($ _ FILES ['UploadedFile的'] ['名称']);

    如果(move_uploaded_file($ _ FILES ['UploadedFile的'] ['tmp_name的值'],$ target_path))
    {
        回声文件.basename($ _ FILES ['UploadedFile的'] ['名称'])已上载。
    }
    其他
    {
        回声发生错误上传文件,请重试!;
    }
?>
 
androidCode的个人空间 OSCHINA

我使用的是Android code:

 公共无效videoUpload()
{
    HttpURLConnection的连接= NULL;
    DataOutputStream类的OutputStream = NULL;
    的DataInputStream的InputStream = NULL;


    字符串pathToOurFile =/sdcard/video-2010-03-07-15-40-57.3gp;
    字符串URL服务器=htt​​p://10.0.0.15/sampleWeb/handle_upload.php;
    字符串lineEnd =\ r \ N的;
    串twoHyphens = - ;
    字符串边界=*****;

    INT读取动作,方bytesAvailable,缓冲区大小;
    byte []的缓冲区;
    INT maxBufferSize = 1 * 1024 * 1024;

    尝试
    {
    的FileInputStream的FileInputStream =新的FileInputStream(新文件(pathToOurFile));

    网址URL =新的网址(URL服务器);
    连接=(HttpURLConnection类)url.openConnection();

    //允许输入和放大器;输出
    connection.setDoInput(真正的);
    connection.setDoOutput(真正的);
    connection.setUseCaches(假);

    //启用POST方法
    connection.setRequestMethod(POST);

    connection.setRequestProperty(连接,保持活动);
    connection.setRequestProperty(内容类型,多部分/格式数据;边界);

    的OutputStream =新DataOutputStream类(connection.getOutputStream());
    outputStream.writeBytes(twoHyphens +边界+ lineEnd);
    outputStream.writeBytes(内容处置:表格数据;名称= \UploadedFile的\;文件名= \+ pathToOurFile);
    outputStream.writeBytes(lineEnd);

    方bytesAvailable = fileInputStream.available();
    BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
    缓冲区=新的字节[BUFFERSIZE]

    //读取文件
    读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);

    而(读取动作大于0)
    {
    outputStream.write(缓冲液,0,BUFFERSIZE);
    方bytesAvailable = fileInputStream.available();
    BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
    读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd);

    从服务器//回应(code和消息)
    INT serverResponse code = connection.getResponse code();
     串serverResponseMessage = connection.getResponseMessage();
     Log.d(服务器code,+ serverResponse code);
     Log.d(serverResponseMessage,+ serverResponseMessage);
    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    赶上(例外前)
    {
        ex.printStackTrace();
    }
}
 

解决方案

我的Java是有点举步维艰,但......

看起来connection.getResponse code为只返回HTTP状态code和connection.getResponseMessage是只返回HTTP状态消息 - 但你的PHP不执行任何操作这些值。您可以试试:

  $ target_path =./upload/;
$ SRC = $ _FILES ['UploadedFile的'] ['名称'];

。$ target_path =基名($ SRC);

如果(file_exists($ SRC)
        &功放;&安培;
        &功放;&安培; move_uploaded_file($ SRC,$ target_path)
   ){
    回声文件.basename($ _ FILES ['UploadedFile的'] ['名称'])已上载。
} 其他 {
    标题(服务器错误,真实,503);
    回声发生错误上传文件,请重试!;
    $味精=SRC大小?
       。文件大小($ SRC)。 \ñDEST目录可写的?
       。 is_writeable(目录名($ target_path))? Y \ N:N \ N
       。 文件所包含:\ N的;
       。 var_export($ _ FILES,真正的);
    //现在写$味精somwhere你可以阅读
}
 

这shuld在缩小了什么差错帮助

I am trying to upload video from Android code to my PHP web server code but can't get success. I am referring the following link to perform the uploading task but I am getting the following response in my Android code, but can't found any file on PHP server.

Android response

DEBUG/ServerCode(29484): 200
DEBUG/serverResponseMessage(29484): OK

I have checked many things like setting values in php.ini files. Although I can upload images from Android code to server becasue from Android I am sending a 64 bit encoded ByteArray and therer is a ready-made function in php which can create image from encoded ByteArrays.

But that code also is not working in case of any other file having type other than image.

Please guide me if any of you have done something similar before.

PHP code I am using:

<?php

    $target_path  = "./upload/";

    $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!";
    }
?>

Android code I am using:

public void videoUpload()
{
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;


    String pathToOurFile = "/sdcard/video-2010-03-07-15-40-57.3gp";
    String urlServer = "http://10.0.0.15/sampleWeb/handle_upload.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");

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile );
    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("ServerCode",""+serverResponseCode);
     Log.d("serverResponseMessage",""+serverResponseMessage);
    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

解决方案

My java is a bit ropey but....

It looks like connection.getResponseCode is returning only the HTTP status code, and connection.getResponseMessage is returning only the HTTP status message - but you're PHP does nothing to manipulate those values. You might try:

$target_path  = "./upload/";
$src = $_FILES['uploadedfile']['name'];

$target_path .= basename($src);

if(file_exists($src) 
        && 
        && move_uploaded_file($src, $target_path)
   ) {
    echo "The file ".basename( $_FILES['uploadedfile']['name'])." has been uploaded";
} else {
    header("Server Error", true, 503);
    echo "There was an error uploading the file, please try again!";
    $msg = "src size ? " 
       . filesize($src) . "\n dest dir writable ?"
       . is_writeable(dirname($target_path)) ? "Y\n" : "N\n"
       . "FILES contains :\n";
       . var_export($_FILES,true);
    // now write $msg somwhere you can read it
}

This shuld help in narrowing down what went wrong