2015-10-04 36 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

相关问题