将文件上载到服务器

时间:2017-02-24 20:16:54

标签: java php android post httpurlconnection

我试图开发一个在录制后在服务器上上传一些文件音乐的应用。 但每次我想上传的东西,我得到我的服务器返回的错误500,我不知道为什么......你能帮帮我吗?谢谢 为此我使用这个类:

protected String doInBackground(String... params) {
        String PATH = params[0];
        String NOM = params[1];
        File file = new File(PATH);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            String aaa = "http://192.168.137.102/php";
            URL url = new URL(aaa +"/son/upload.php");
            HttpURLConnection 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");
            String boundary = "*****";
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploaded_file", file.getName());
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
            String twoHyphens = "--";
            String lineEnd = "\r\n";
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + file.getName() + "\"" + lineEnd);
            Log.d("File Name: ", file.getName());
            int bytesAvailable = fileInputStream.available();

            int maxBufferSize = 100 * 1024 * 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bufferSize];
            int 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);

            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            int serverResponseCode = conn.getResponseCode();
            String sResponse = conn.getResponseMessage();

            Log.i("uploadFile", "HTTP Response is : "
                    + sResponse + ": " + serverResponseCode);

            if(serverResponseCode == 200){
                if(sResponse.equalsIgnoreCase("OK")){
                    Log.d("Response", "ok");
                    return "1";
                }else{
                    return "-2";
                }
            }

            //close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }

这是php:

<?php
    $file_path = "/";

    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
 ?>

最后我的logcat:

I/Enregistrement: terminé
I/filename: /storage/emulated/0/Whizz/20_24_1_2017.mp3
I/nomfinale: 20_24_1_2017
D/File Name:: 20_24_1_2017.mp3
I/uploadFile: HTTP Response is : Internal Server Error: 500

我对stackoverflow进行了大量的研究,但是所有的aswers并没有给我很好的帮助

编辑:它看起来像是不能正常工作的php文件..这是日志: 我已经检查了我的php日志,这是我的错误:"Failed opening required '/var/www/html/php/son/upload.php' (include_path=".:/usr/share/php') in Unknown on line 0

0 个答案:

没有答案