Java 尝试将sqlite数据库从android手机发送到web服务器
我正在尝试将一个sqlite数据库从我的android手机发送到一个web服务器。当代码执行时,我没有收到任何错误,但是数据库没有出现在服务器上。下面是我的php代码和从android手机上传文件的代码。连接响应消息是get是“OK”,来自http客户机的响应是org.apache.http.message。BasicHttpResponse@4132dd40.Java 尝试将sqlite数据库从android手机发送到web服务器,java,android,database,upload,Java,Android,Database,Upload,我正在尝试将一个sqlite数据库从我的android手机发送到一个web服务器。当代码执行时,我没有收到任何错误,但是数据库没有出现在服务器上。下面是我的php代码和从android手机上传文件的代码。连接响应消息是get是“OK”,来自http客户机的响应是org.apache.http.message。BasicHttpResponse@4132dd40. public void uploadDatabase() { String urli = "http://uplo
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";
}
?>
public void uploadDatabase(){
字符串urli=”http://uploadsite.com";
字符串路径=sql3.getPath();
文件=新文件(路径);
int字节读取,字节可用,缓冲区大小;
字节[]缓冲区;
int maxBufferSize=1*1024*1024;
字符串lineEnd=“\r\n”;
字符串双连字符=“--”;
字符串边界=“*******”;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(urli);
URL=新URL(urli);
connection=(HttpURLConnection)url.openConnection();
InputStreamEntity reqEntity=新的InputStreamEntity(
新文件输入流(文件),-1);
requentity.setContentType(“二进制/八位字节流”);
reqEntity.setChunked(真);
HttpResponse response=httpclient.execute(httppost);
字符串response2=connection.getResponseMessage();
Log.i(“response”,response.toString());
Log.i(“response”,response2.toString());
}捕获(例外e){
}
}
我基于这个示例编写了代码,效果很好
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();
}
你让它工作了吗。如果是的话,怎么办。我需要做同样的事情。是的,我没有确切的代码,但我在下面放了一个我基于它的示例。