2

我正在尝试将 sqlite 数据库从我的 android 手机发送到 Web 服务器。代码执行时我没有收到任何错误,但是数据库没有出现在服务器上。这是我的 php 代码和从 android 手机上传文件的代码。连接响应消息是“OK”,我得到的来自 http 客户端的响应是 org.apache.http.message.BasicHttpResponse@4132dd40。

    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";
}

?>
4

1 回答 1

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();
}
于 2012-05-10T18:30:58.917 回答