解决方案 »

  1.   

    你是想文件到服务器端吧,看你服务器的接口,用普通的HTTP方式 还是 WEB SERVICE都可以,看服务器端提供的上传接口
      

  2.   

    服务器提供api,客户端直接调用,传递参数,把图片以流的形式传递.
      

  3.   

    /** 上传文字加图片 */
    public void postDataFile(String serverUrl, String fileurl, String data) {
        Log.e("test", "load" + serverUrl);
        Log.e("test", "load" + fileurl);
        Log.e("test", "load" + data);
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(serverUrl);
        MultipartEntity mpEntity = new MultipartEntity();
        try {
     
            File file = new File(fileurl);
            FileBody fileBody = new FileBody(file);
            mpEntity.addPart("file", fileBody);
            mpEntity.addPart(
                    "data",
                    new StringBody(data, Charset
                            .forName(org.apache.http.protocol.HTTP.UTF_8)));
     
            post.setEntity(mpEntity);
            HttpResponse response = client.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(),
                        "utf-8");
                ho.handleEvent(content);//回调函数
                 
            }
            Log.e("test", "IsOk");
        } catch (Exception e) {
            Log.e("test", "出错了:"+e);
     
        } finally {
            if (mpEntity != null) {
                try {
                    mpEntity.consumeContent();
                } catch (UnsupportedOperationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
     
            }
            client.getConnectionManager().shutdown();
        }
         
    }
      

  4.   

    public class UploadUtil {

    private static final String TAG = "uploadFile";    
    private static final int TIME_OUT = 10 * 1000; // 超时时间  
    private static final String CHARSET = "utf-8"; // 设置编码 
      public  static int uploadFile(File file, String RequestURL,Context context) { 
      int res=0;         String result = null;         
      String BOUNDARY = UUID.randomUUID().toString(); 
      // 边界标识 随机生成         
      String PREFIX = "--", LINE_END = "\r\n";        
      String CONTENT_TYPE = "multipart/form-data"; // 内容类型 
      try {         
      URL url = new URL(RequestURL);           
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();            
      conn.setReadTimeout(TIME_OUT);            
      conn.setConnectTimeout(TIME_OUT);         
      conn.setDoInput(true); // 允许输入流            
      conn.setDoOutput(true); // 允许输出流         
      conn.setUseCaches(false); // 不允许使用缓存             
      conn.setRequestMethod("POST"); // 请求方式             
      conn.setRequestProperty("Charset", CHARSET); // 设置编码             
      conn.setRequestProperty("connection", "keep-alive");            
      conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary="+ BOUNDARY);
      Toast.makeText(context, "--------------------"+file.getPath(), 3).show();  
      if (file !=null) {                 /**                  * 当文件不为空时执行上传                  */  
      Toast.makeText(context, "--name----"+file.getName(), 3).show();  
      DataOutputStream dos = new DataOutputStream(conn.getOutputStream());          
      StringBuffer sb = new StringBuffer();                 
      sb.append(PREFIX);                
      sb.append(BOUNDARY);               
      sb.append(LINE_END); 
      
      sb.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file.getName()+"\""+LINE_END);               
      sb.append("Content-Type: application/octet-stream; charset="+CHARSET+LINE_END);         
      sb.append(LINE_END);                
      dos.write(sb.toString().getBytes());                
      InputStream is = new FileInputStream(file);                
      byte[] bytes = new byte[2048];                 
      int len = 0;                
      while ((len = is.read(bytes)) != -1) {                  
      dos.write(bytes, 0, len);           
      }               
      is.close();                 dos.write(LINE_END.getBytes());              
      byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END) .getBytes();            
      dos.write(end_data);                 
      dos.flush(); 
       res = conn.getResponseCode();                
       Toast.makeText(context, "上传成功"+res, 3);                 
       if (res==200) {                    
       Log.e(TAG, "request success");                    
       InputStream input = conn.getInputStream();                    
       StringBuffer sb1 = new StringBuffer();                   
       int ss;                     
       while ((ss = input.read()) != -1) {                   
       sb1.append((char) ss);                   
       }                    
       result = sb1.toString();                    
       Toast.makeText(context, "上传成功", 3).show();                
       } else {             
       Toast.makeText(context, "上传成功-----", 3).show();   
       }           
       } 
      }
      catch (MalformedURLException e) {           
      e.printStackTrace();    
      Toast.makeText(context, "上传失败+++", 3).show();  
      } catch (IOException e) {           
      e.printStackTrace();   
      }         
      
      return res; 
      }