Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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/3/android/217.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
使用PHP脚本通过POST请求方法将文本文件上载到服务器上载的文件中存在Unicode字符问题_Php_Android_Post_Utf 8 - Fatal编程技术网

使用PHP脚本通过POST请求方法将文本文件上载到服务器上载的文件中存在Unicode字符问题

使用PHP脚本通过POST请求方法将文本文件上载到服务器上载的文件中存在Unicode字符问题,php,android,post,utf-8,Php,Android,Post,Utf 8,在Android应用程序中 我使用以下代码使用POST请求方法将txt文件上载到服务器: public static String ServiceCall_TextCommand(String mFileName, String mFileDestination, String mFileData) { int serverResponseCode = 0; String fileName = mFileName; String

在Android应用程序中

我使用以下代码使用
POST
请求方法将txt文件上载到服务器:

    public static String ServiceCall_TextCommand(String mFileName, String mFileDestination, String mFileData) 
    {
        int serverResponseCode = 0;
        String fileName = mFileName;

        String upLoadServerUri = null;
        /************* Php script path ****************/
        upLoadServerUri = mFileDestination;

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

        try {
            URL url = new URL(upLoadServerUri);

            // 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("uploaded_file", fileName);

            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);

            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\""
                    + fileName + "\"" + lineEnd);


            dos.writeBytes(lineEnd);

            bufferSize = mFileData.length();
            buffer = mFileData.getBytes(Charset.forName("UTF-8"));

            dos.write(buffer, 0, bufferSize);
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("BBBuploadFile", "HTTP Response is : "
                    + serverResponseMessage + ": " + serverResponseCode);

            if (serverResponseCode == 200) {

                Log.e(TAG, "Complete");
            }
            else
            {
                return  Constants.ServerResponse_Error_General;
            }

            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null)
            {
                total.append(line).append('\n');
            }

            dos.flush();
            dos.close();

            // Verify Command Response Checksum
            String mServerResponse = total.toString();
            mServerResponse = getStringBetween(mServerResponse,"@#@","#@#");
  
            return mServerResponse;

        } catch (MalformedURLException ex) {

            ex.printStackTrace();
            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
            return Constants.ServerResponse_Error_General;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "111Exception: " + e.getMessage());
            return Constants.ServerResponse_Error_General;
        }
    }
服务器端PHP代码如下:

<?php
    
    header('Content-type: text/plain');
    
    require_once $ServerRoot.'/HandleUploadedFile/HandleUploadedFile.php';
    use HandleUploadedFile\HandleUploadedFile_Status;   
    
    print "\nFile Received, Root: {$_SERVER['DOCUMENT_ROOT']}\nName: {$_FILES['uploaded_file']['name']}\nSize: {$_FILES['uploaded_file']['size']}";
    echo "\nprint_r:\n";
    print_r($_FILES);

    $mClass_HandleUploadedFile_Status = new HandleUploadedFile_Status();
    if ($mClass_HandleUploadedFile_Status->IsUploadedFileValid() === true)
    {
        echo "\File is valid\n"; 
        echo "\nupload successful";
        
        $File_Name = $_FILES['uploaded_file']['name'];
        $File_SplitName = explode(".", $File_Name);
        $File_Name_NoExtension = $File_SplitName[0];
        $File_Extension = end($File_SplitName);

        $ReadFile_Resource = fopen($_FILES['uploaded_file']['tmp_name'], "r");
        $ReadFile = fread($ReadFile_Resource, filesize($_FILES['uploaded_file']['tmp_name']));
        

        $fz = filesize($_FILES['uploaded_file']['tmp_name']);
        echo "\n*#*\nfz :\n{$fz}\n#*###\n";
        
        echo "\n\nReadFile :\n{$ReadFile}\n\n";
        
        fclose($ReadFile_Resource);        
    }

您上传的不是txt文件,而是字符串mFileData。完全不同。如果你想上传一个文件,那么不要先把内容放在字符串中。您应该以这样一种方式编写代码,即您也可以使用相同的代码上载图像。或pdf格式。因为文件就是文件。一些字节。一个代码适合所有人。我也编写了上载图像的代码,但问题是您提到的字符串中存在Unicode问题您正在上载字符串。我已经说过了。不要先用字符串把文件放进去。仅上载文件的字节。
<?php
    namespace HandleUploadedFile;
    class HandleUploadedFile_Status
    {
        public function IsUploadedFileValid()
        {
            if($this->UploadedFileExists() === true)
            {
                if($this->UploadedFileKeyArrayExists() === true)
                {
                    if($this->UploadedFileIsErrorOk() === true)
                    {
                        return true;
                    }
                    else 
                    {
                        return false;
                    }      
                }
                else 
                {
                    return false;
                }                
            }
            else 
            {
                return false;
            }
        }
        
        public function UploadedFileExists()
        {
            if(isset($_FILES["uploaded_file"]))
            {
                return true;
            }
            else 
            {
                return false;
            }
        }

        public function UploadedFileKeyArrayExists()
        {
            if(array_key_exists('uploaded_file', $_FILES))
            {
                return true;
            }
            else 
            {
                return false;
            }
        }

        public function UploadedFileIsErrorOk()
        {
            if($_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }        
    }