2015-10-04 38 views
0

我用下面的代碼發送圖像/視頻到PHP服務器Android的PHP發送字符串非常久遠的多數據

<?php 

    $file_path = "uploads/"; 

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

現在,我也想發送一個字符串到服務器。

例子: - 名稱:「用戶」,值:「ABC」

這樣,我應該能夠在PHP取爲:

$value = $_POST["user"] 

試問,這樣的字符串添加到outputstream在PHP中接收?

+0

關於以同樣的方式像你一樣與文件:在兩個邊界之間。您的文件內容只是多部分數據的一部分。您可以根據需要添加任意數量的部件。零件按邊界分開。 – greenapps

+0

@greenapps你能告訴我怎麼做?我需要爲字符串添加setRequestProperty()嗎?我需要Content-Disposition嗎? – user5155835

回答

1

我做到了使用:

FileInputStream fileInputStream = new FileInputStream(sourceFile); 
URL url = new URL(upLoadServerUri); 

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"); 
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
conn.setRequestProperty("uploaded_file", fileName); 

DataOutputStream 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); 

// create a buffer of maximum size 
int bytesAvailable = fileInputStream.available(); 

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

// read file and write it into form... 
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); 

} 

// send multipart form data necesssary after file data... 
dos.writeBytes(lineEnd); 

dos.writeBytes(twoHyphens + boundary + lineEnd); 
dos.writeBytes("Content-Disposition: form-data; name=\"user\"" + lineEnd); 

dos.writeBytes(lineEnd); 

dos.writeBytes(this_number); 

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("uploadFile", "HTTP Response is : " 
     + serverResponseMessage + ": " + serverResponseCode); 
0

一個做的方式是通過獨特的分離器($

Content-Disposition: form-data; name="uploaded_file";filename=""+ fileName +"_$_stringdata"+ """ + lineEnd 

在PHP中分離內容處置一起發送字符串,http://php.net/manual/en/httpresponse.getcontentdisposition.php

使用getContentDisposition並解析它。

Description 

static string HttpResponse::getContentDisposition (void) 
Get current Content-Disposition setting. 


其他方式:使用httpmime 只需添加一個幾FormBodyPart您MultipartEntity。

您可以使用StringBody對象來提供值。

這裏是你如何使用它的一個例子:

byte[] data = {10,10,10,10,10}; 
HttpClient httpClient = new DefaultHttpClient(); 
HttpPost postRequest = new HttpPost("server url"); 
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg"); 
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
reqEntity.addPart("image", bab); 

FormBodyPart bodyPart=new FormBodyPart("formVariableName", new StringBody("formValiableValue")); 
reqEntity.addPart(bodyPart); 
bodyPart=new FormBodyPart("formVariableName2", new StringBody("formValiableValue2")); 
reqEntity.addPart(bodyPart); 
bodyPart=new FormBodyPart("formVariableName3", new StringBody("formValiableValue3")); 
reqEntity.addPart(bodyPart); 
postRequest.setEntity(reqEntity); 
HttpResponse response = httpClient.execute(postRequest); 
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
String line = null; 
while((line = in.readLine()) != null) { 
    System.out.println(line); 
} 

在PHP中:

$_FILES 
Array 
(
    [image] => Array 
     (
      [name] => image.jpg 
      [type] => application/octet-stream 
      [tmp_name] => /tmp/php6UHywL 
      [error] => 0 
      [size] => 5 
     ) 

) 
$_POST: 
Array 
(
    [formVariableName] => formValiableValue 
    [formVariableName2] => formValiableValue2 
    [formVariableName3] => formValiableValue3 
) 
+0

請您告訴我們代碼? – user5155835

+0

@ user5155835我已經更新了我的答案:) – AskQ

+0

我希望避免MultipartEntity,所以使用HttpURLConnection時,可以使用HttpURLConnection告訴解決方案嗎? – user5155835