2012-09-21 74 views
2

如何從Android移動圖像到指定的文件夾中的Web服務器上如何上傳從Android應用程序圖像到網絡服務器到一個特定的文件夾

這是我的Android代碼

package com.example.bitmaptest; 

import java.io.ByteArrayOutputStream; 
import java.io.InputStream; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Base64; 
import android.util.Log; 

public class Upload extends Activity { 
InputStream is; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Bitmap bitmapOrg=BitmapFactory.decodeResource(getResources(),R.drawable.a1); 
    ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); 
    byte[] ba = bao.toByteArray(); 
    String ba1 = Base64.encodeToString(ba, 0); 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("image", ba1)); 

    try { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(
       "http://0.0.0.7:80/android/base.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    } catch (Exception e) { 
     Log.e("log_tag", "Error in http connection " + e.toString()); 
    } 

} 
} 

,這是我的php腳本

$uploadedFile = $_FILES['image']['name']; 

$uploadedType = $_FILES['image']['type']; 

$temp = $_FILES['image']['tmp_name']; 

$error = $_FILES['image']['error']; 

if ($error > 0) { 
die("File could not be uploaded. $error"); 
} 
else { 
    move_uploaded_file($temp, "images/".$uploadedFile); 
    echo "Upload Complete. ".$uploadedType; 
} 

當我發送android,httppost工作,它顯示wireshark但文件是空的。沒有圖像。

+0

在$ uploadedSize多大? – MrSil

+0

您的代碼無法正常工作的主要原因是,您沒有將後請求的內容類型設置爲「multipart/form-data」。看到下面的Nirav Ranpara的答案,那應該對你有所幫助。 – Ridcully

+0

因此base64無法正常工作?base64成功時,圖像保存在根目錄下。不是指定的文件夾。當我移動指定的文件夾theres沒有圖像,爲什麼multipart? –

回答

1

doFileUpload功能:

private void doFileUpload(){ 
    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    DataInputStream inStream = null; 
    String exsistingFileName = "/sdcard/six.3gp"; 
    // Is this the place are you doing something wrong. 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1*1024*1024; 
    String urlString = "http://192.168.1.5/upload.php"; 
    try 
    { 
     Log.e("MediaPlayer","Inside second Method"); 
     FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName)); 
     URL url = new URL(urlString); 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setDoInput(true); 
     // Allow Outputs 
     conn.setDoOutput(true); 
     // Don't use a cached copy. 
     conn.setUseCaches(false); 
     // Use a post method. 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Connection", "Keep-Alive"); 
     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
     dos = new DataOutputStream(conn.getOutputStream()); 
     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd); 
     dos.writeBytes(lineEnd); 
     Log.e("MediaPlayer","Headers are written"); 
     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     buffer = new byte[bufferSize]; 
     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); 
     BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     String inputLine; 
     while ((inputLine = in.readLine()) != null) 
      tv.append(inputLine); 
     // close streams 
     Log.e("MediaPlayer","File is written"); 
     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 
    } 
    catch (MalformedURLException ex) 
    { 
     Log.e("MediaPlayer", "error: " + ex.getMessage(), ex); 
    } 
    catch (IOException ioe) 
    { 
     Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe); 
    } 

    //------------------ read the SERVER RESPONSE 
    try { 
     inStream = new DataInputStream (conn.getInputStream()); 
     String str;    
     while ((str = inStream.readLine()) != null) 
     { 
      Log.e("MediaPlayer","Server Response"+str); 
     } 
     /*while((str = inStream.readLine()) !=null){ 

     }*/ 
     inStream.close(); 
    } 
    catch (IOException ioex){ 
     Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); 
    } 
} 
+0

但是im導入httpclient和httpmime jar –

+0

這個demo還可以工作..你可以先試試。 –

相關問題