android视频mp4上传到php服务器

时间:2017-07-16 11:00:52

标签: java php android

你好我无法将视频文件上传到我的服务器我正在使用的代码工作之前现在它在我切换服务器后不再有效我不知道问题是什么请帮助 上传代码:

private class sendvid extends AsyncTask<String, String, String> {
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "reelyChat/vids/"+vid_name);
    @Override
    protected String doInBackground(String... urls) {

        File mediaFile = new File(mediaStorageDir.getPath() + File.separator + vid_name);


        String url = urls[0];
        uploadvid(url, mediaStorageDir);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        newVideo = mediaStorageDir;
        uploadIcon.setVisibility(View.GONE);
    }
}

public int uploadvid(String SERVER_URL, File sourceFileUri){
    int serverResponseCode = 200;

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "------------------------afb19f4aeefb356c";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = sourceFileUri;

    if (!sourceFile.isFile()) {
        return 0;  //RETURN #1
    }
    else{
        try{
            Log.v("joshtag","UPLOADING .WAV FILE");
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(SERVER_URL);
            Log.v("joshtag","UL URL: "+url.toString());

            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy            
            conn.setRequestMethod("POST");
            // conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("file", sourceFile.getName());

            conn.setRequestProperty("user", "value_1");

            //so on and so forth...
            //conn.setRequestProperty("param", "value");
            conn.setRequestProperty("connection", "close");
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + sourceFile.getName() + "\"" + lineEnd);
            dos.writeBytes(lineEnd);

            // create a buffer of  maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while(bytesRead > 0){
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                Log.i("joshtag","->");
            }
            Log.i("joshtag","->->");
            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            conn.connect();
            Log.i("joshtag","->->->");
            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            Log.i("joshtag","->->->->");
            String serverResponseMessage = conn.getResponseMessage().toString();

            Log.i("joshtag","->->->->->");
            Log.i("joshtag", "HTTP Response is : "  + serverResponseMessage + ": " + serverResponseCode);

            // ------------------ read the SERVER RESPONSE
            BufferedReader inStream;
            String str="";
            String response="";
            try{
                Log.i("joshtag","->->->->->->");
                inStream = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                while((str = inStream.readLine()) != null) {
                    Log.e("joshtag", "SOF Server Response" + str);
                    response = str;
                }
                inStream.close();
            }
            catch (IOException ioex) {
                Log.e("joshtag", "SOF error: " + ioex.getMessage(), ioex);
            }


            conn.disconnect();



            conn = null;
            //close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

            if(serverResponseCode == 201){
               Log.e("*** SERVER RESPONSE: 201" + response);
            }//END IF Response code 201
            // conn.disconnect();
        }//END TRY - FILE READ
        catch (MalformedURLException ex) {
            ex.printStackTrace();
            Log.e("joshtag", "UL error: " + ex.getMessage(), ex);
        } //CATCH - URL Exception

        catch (Exception e) {
            e.printStackTrace();
            Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
        }

        return serverResponseCode; //after try
    }//END ELSE, if file exists.
}

php对应:

$vid_name = $_FILES["file"]["name"];
$split = strtok($vid_name, "_");
$split = strtok("_");
$user = $split;

$target_dir = "../users/$user/profileVid/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);

这是logcast:

07-14 04:26:47.822 24150-24170/com.reelychat.reelychat I/dalvikvm: "AsyncTask #1" prio=5 tid=11 RUNNABLE
07-14 04:26:47.822 24150-24170/com.reelychat.reelychat I/dalvikvm:   | group="main" sCount=0 dsCount=0 obj=0x41e9d360 self=0x5012c6d0
07-14 04:26:47.822 24150-24170/com.reelychat.reelychat I/dalvikvm:   | sysTid=24170 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1074255192
07-14 04:26:47.822 24150-24170/com.reelychat.reelychat I/dalvikvm:   | state=R schedstat=( 0 0 0 ) utm=27 stm=17 core=1
07-14 04:26:47.822 24150-24170/com.reelychat.reelychat I/dalvikvm:     at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~91)
07-14 04:26:47.822 24150-24170/com.reelychat.reelychat I/dalvikvm:     at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
07-14 04:26:47.822 24150-24170/com.reelychat.reelychat I/dalvikvm:     at libcore.net.http.RetryableOutputStream.write(RetryableOutputStream.java:61)
07-14 04:26:47.822 24150-24170/com.reelychat.reelychat I/dalvikvm:     at java.io.DataOutputStream.write(DataOutputStream.java:98)
07-14 04:26:47.832 24150-24170/com.reelychat.reelychat I/dalvikvm:     at com.reelychat.reelychat.ProfileUser.uploadvid(ProfileUser.java:1027)
07-14 04:26:47.832 24150-24170/com.reelychat.reelychat I/dalvikvm:     at com.reelychat.reelychat.ProfileUser$sendvid.doInBackground(ProfileUser.java:1370)
07-14 04:26:47.832 24150-24170/com.reelychat.reelychat I/dalvikvm:     at com.reelychat.reelychat.ProfileUser$sendvid.doInBackground(ProfileUser.java:1357)
07-14 04:26:47.832 24150-24170/com.reelychat.reelychat I/dalvikvm:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-14 04:26:47.842 24150-24170/com.reelychat.reelychat I/dalvikvm:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-14 04:26:47.842 24150-24170/com.reelychat.reelychat I/dalvikvm:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-14 04:26:47.842 24150-24170/com.reelychat.reelychat I/dalvikvm:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-14 04:26:47.842 24150-24170/com.reelychat.reelychat I/dalvikvm:     at java.lang.Thread.run(Thread.java:856)
07-14 04:26:47.842 24150-24170/com.reelychat.reelychat W/dalvikvm: threadid=11: thread exiting with uncaught exception (group=0x41533930)

07-14 04:26:47.842 24150-24170/com.reelychat.reelychat E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
at libcore.net.http.RetryableOutputStream.write(RetryableOutputStream.java:61)
at java.io.DataOutputStream.write(DataOutputStream.java:98)
at com.reelychat.reelychat.ProfileUser.uploadvid(ProfileUser.java:1027)
at com.reelychat.reelychat.ProfileUser$sendvid.doInBackground(ProfileUser.java:1370)
at com.reelychat.reelychat.ProfileUser$sendvid.doInBackground(ProfileUser.java:1357)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
at java.lang.Thread.run(Thread.java:856) 

0 个答案:

没有答案