我正在尝试将 sqlite 数据库从我的 android 手机发送到网络服务器。代码执行时我没有收到任何错误,但是数据库没有出现在服务器上。这是我的 php 代码和从 android 手机上传文件的代码。连接响应消息是“确定”,我收到的 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";
}
?>
最佳答案
我的代码基于此示例,它运行良好。
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();
}
关于java - 试图将 sqlite 数据库从 android 手机发送到 web 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10272300/