2

AndroidフォンからWebサーバーにsqliteデータベースを送信しようとしています。コードの実行時にエラーは発生しませんが、データベースがサーバーに表示されません。これが私の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 に答える