2010-12-09 74 views

回答

0

使用的HttpClient在web服務器的圖像連接到您的網絡服務器從客戶端。我做了這樣的連接到PHP服務:

/** 
* Transmits a file from the local filesystem to a web-server 
* @param fileName the local file to transmit 
* @param params the remote parameters for the php script 
* @return the server response as a String 
*/ 
public String httpUploadFile(String fileName, String params) { 
    if(fileName==null) { 
     return "ERROR file is null"; 
    } else { 
     DDLog.i(TAG, "Initiating httpUpload for file "+fileName); 
     HttpClient httpClient = new DefaultHttpClient(); 
     httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(300000)); // 300 seconds -> 5 min 
     httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

     String url = this.baseURL + "?" + params; 
     HttpPost httpPost = new HttpPost(url); 

     MultipartEntity mpEntity = new MultipartEntity(); 
     File file = new File(fileName); 
     double fileBytes = file.length(); 
     DecimalFormat decimalFormat = new DecimalFormat("0.000"); 
     Log.i(TAG, "httpUpload file: "+fileName+" with "+file.length()+" bytes; "+decimalFormat.format(fileBytes/1024)+" kb; "+decimalFormat.format(fileBytes/1048576)+" mb"); 

     FileBody bodyFile = new FileBody(file); 
     mpEntity.addPart("pic", bodyFile); 

     httpPost.setEntity(mpEntity); 

     HttpResponse httpResponse = null; 
     HttpEntity responseEntity = null; 
     String response = ""; 
     try { 
      httpResponse = httpClient.execute(httpPost); 
      responseEntity = httpResponse.getEntity(); 
      response = EntityUtils.toString(responseEntity); 

     } catch (SocketException e) { 
      e.printStackTrace(); 
      DDLog.e(TAG, e.getMessage(), e); 
      response = "SOCKET EXCEPTION " + e.getMessage(); 

     } catch (SocketTimeoutException e) { 
      e.printStackTrace(); 
      DDLog.e(TAG, e.getMessage(), e); 
      response = "SOCKET TIMEOUT"; 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
      DDLog.e(TAG, e.getMessage(), e); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      DDLog.e(TAG, e.getMessage(), e); 
     } 

     DDLog.i(TAG, "HttpResponse: "+response); 

     return response; 
    } 
} 

但是你應該提供你將如何做一些更多的信息,例如您在您的網絡服務器上使用哪種技術

+0

我使用jsp概念 – ragu 2010-12-09 09:49:26

0

您必須將數據作爲android應用中的輸出流發送,並在servlet中作爲輸入流從請求對象中讀取。