2010-12-09 59 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中作为输入流从请求对象中读取。