2012-04-22 93 views
2

我想從我的android手機發送一個sqlite數據庫到一個web服務器。代碼執行時不會出現錯誤,但數據庫不會顯示在服務器上。這裏是我的php代碼和代碼從android手機上傳文件。連接響應消息get是「OK」,我從http客戶端得到的響應是[email protected]試圖從android手機發送sqlite數據庫到web服務器

public void uploadDatabase() { 


    String urli = "http://uploadsite.com"; 
       String path = sql3.getPath(); 
       File file = new File(path); 

      int bytesRead, bytesAvailable, bufferSize; 
      byte[] buffer; 
      int maxBufferSize = 1*1024*1024; 
      String lineEnd = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "*****"; 


      try { 
      HttpClient httpclient = new DefaultHttpClient(); 

      HttpPost httppost = new HttpPost(urli); 

      URL url = new URL(urli); 
      connection = (HttpURLConnection) url.openConnection(); 

      InputStreamEntity reqEntity = new InputStreamEntity(
        new FileInputStream(file), -1); 

      reqEntity.setContentType("binary/octet-stream"); 
      reqEntity.setChunked(true); 

      HttpResponse response = httpclient.execute(httppost); 
      String response2 = connection.getResponseMessage(); 
      Log.i("response", response.toString()); 
      Log.i("response", response2.toString()); 
     } catch (Exception e) { 

     } 
    } 


<?php 
$uploaddir = '/var/www/mvideos/uploads/'; 
$file = basename($_FILES['userfile']['name']); 
$timestamp = time(); 
$uploadfile = $uploaddir . $timestamp . '.sq3'; 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
    echo "OK"; 
} else { 
    echo "ERROR: $timestamp"; 
} 

?> 
+0

你是否得到這個工作。如果是這樣的話。我需要做同樣的事情。 – lastshadowrider 2012-05-09 19:25:30

+0

是的,我沒有確切的代碼,但我把下面的樣本放在了我的基礎上。 – johns4ta 2012-05-10 18:27:03

回答

1

我基於我的代碼在這個例子,它工作正常。

String pathToOurFile = "/data/dada.jpg"; 
String urlServer = "http://sampleserver.com"; 

try { 
    FileInputStream fis = new FileInputStream(new File(pathToOurFile)); 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost postRequest = new HttpPost(urlServer); 
    byte[] data = IOUtils.toByteArray(fis); 
    InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data),pathToOurFile); 
    StringBody sb1 = new StringBody("someTextGoesHere"); 
    StringBody sb2 = new StringBody("someTextGoesHere too"); 
    MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
    FileBody bin = new FileBody(new File(pathToOurFile)); 

    multipartContent.addPart("uploadedfile", bin); 
    multipartContent.addPart("name", sb1); 
    multipartContent.addPart("status", sb2); 
    postRequest.setEntity(multipartContent); 

    HttpResponse res = httpClient.execute(postRequest); 
    res.getEntity().getContent().close(); 
} catch (Throwable e) { 
    e.printStackTrace(); 
} 
相關問題