2012-06-22 22 views
6

我在android上傳圖像。目前我的代碼只上傳文件,但我也想發送一些參數。我正在嘗試以下如何發送參數與android中的文件

FileInputStream fileInputStream = new FileInputStream(sourceFile); 
     URL url = new URL(upLoadServerUri); 
     conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL 
     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); 

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

並在服務器端我使用php。這裏是我如何獲得該參數

$param = $_POST["paramName"]; 
target_path1 = "./places_photos/" . $param; 

但我目前的代碼確實上傳文件,但它不發送參數。我怎樣才能發送參數,我怎樣才能讓他們在服務器端?

更新

目前,圖像保存在它在$target_path1可變提到places_photos目錄。我想要的是將該圖像保存在用戶的目錄中,並且該目錄被命名爲用戶標識。但不幸的是,我沒有得到服務器端的用戶ID。我如何將文件發送到服務器?

回答

0

還應該有一個lineEnd在此之後說:

dos.writeBytes(twoHyphens + boundary + lineEnd); 
    dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd); 
    dos.writeBytes(lineEnd); 
    dos.writeBytes(globalUID); 
    dos.writeBytes(lineEnd); <-- add this 

另外,還要確保你添加多邊界(即開頭的行「 - 」)結束在你的情況,補充一點:

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

這也不工作。其他解決方案? – 2619

+0

您可以使用像Firebug或Wireshark這樣的工具,並跟蹤發送到服務器的表單是什麼形式?多部分/表單數據對於新線條和邊界100%正確非常挑剔。 – azgolfer

0

你想「讓」php知道你的文件名? 如果您的答案是肯定的,那麼您可以對代碼進行一些更改。

(我假定你已經創建了最大尺寸的緩衝器和讀取文件,並將其寫入形式)

你不需要這個發送第一個變化 PARAMNAME:

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

只是做這樣的:

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

,並在你的PHP,更改代碼:

$param = $_POST["paramName"]; 

$param = basename($_FILES['uploaded_file']['name']); 

,您可以檢查this文章

+0

我想將參數發送到服務器。我的PHP已經知道文件的文件名。 – 2619

+0

如果您只是想將參數發送到服務器,您可以使用HttpClient執行HTTP POST請求 – yosafatade

0

設置在它的用戶ID的cookie。在服務器上,您可以檢索cookie並檢查用戶標識。 (雖然注意這不安全,也沒有傳遞用戶標識參數)

+0

您提到的是一種選擇,但此應用程序要求是通過文件上載將參數發送到服務器。 :( – 2619

0

你的代碼有點混亂,仔細檢查你的變量......例如你的target_path1在前面沒有$,檢查是否有任何其他服務器變量返回任何有效的輸出。

如果全部失敗,則可以重命名正在上載的文件 - 包括文件名中的參數並在提取參數後再次在服務器端重命名它。

0

這很簡單...100%工作解決方案

使用UTF-8在url中發送參數。添加此行

// open a URL connection to the Servlet 
       FileInputStream fileInputStream = new FileInputStream(sourceFile); 
       URL url = new URL("http://your fileaddress.php***?yourphpsideparametername="+URLEncoder.encode(yourperameter, "UTF-8")*** ); 

然後在第二連接請求

   conn.setRequestMethod("POST"); 
       ***conn.setRequestProperty("Accept-Charset", "UTF=8");*** 
       conn.setRequestProperty("Connection", "Keep-Alive"); 

添加此行PHP代碼添加此行

$yourperameter =$_GET['yourparameter']; 
    $file_path = "doc/".$yourperameter."/"; 

    $file_path = $file_path . basename($_FILES['uploaded_file']['name']); 
    $title =$_FILES['title']; 
    //$descr=$file_path . basename($_FILES['uploaded_file']['desc']); 

    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) { 
     echo "success"; 
    } else{ 
     echo "fail"; 
    } 
相關問題