[英]Uploading an SQLite database file from android app using a PHP script
我正在嘗試將 SQLite 數據庫文件“.db”從我的 android 應用程序上傳到 PHP 服務器。 到目前為止,我一直關注這篇文章https://stackoverflow.com/a/25398566 ,我得到的只是來自我的服務器的“200”成功響應,並且文件沒有發布。 我的應用程序或服務器日志中沒有錯誤消息。 這是我上傳的 function:
Android 代碼:
private class UploadFileAsync extends AsyncTask<String, Void, String> {
private Context mContext;
private ContentResolver mContentResolver;
public UploadFileAsync(Context context, ContentResolver resolver) {
mContext = context;
mContentResolver = resolver;
}
@Override
protected String doInBackground(String... params) {
try {
String sourceFileUri = params[0]; //*Uri.fromFile(context.getDatabasePath(myDBHelper.getDatabaseName())).toString();*
String fileName = params[1];
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = mContext.getDatabasePath(myDBHelper.getDatabaseName());
if (sourceFile.isFile()) {
try {
String upLoadServerUri = "https://website.com/script.php?";
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE",
"multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("sqlite", sourceFileUri);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"sqlite\";filename=\""
+ sourceFileUri + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math
.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0,
bufferSize);
}
// send multipart form data necesssary after file
// data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn
.getResponseMessage();
if (serverResponseCode == 200) {
Log.d("uploadFile", "Success > HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
// recursiveDelete(mDirectory1);
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception e) {
// dialog.dismiss();
e.printStackTrace();
}
// dialog.dismiss();
} // End else block
} catch (Exception ex) {
// dialog.dismiss();
ex.printStackTrace();
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
PHP 代碼:
<?php
if (is_uploaded_file($_FILES['sqlite']['tmp_name'])) {
$uploads_dir = './folder';
$tmp_name = $_FILES['sqlite']['tmp_name'];
$db_name = $_FILES['sqlite']['name'];
if(move_uploaded_file($tmp_name, $uploads_dir.$db_name)){
echo 'success';
}else{
echo 'fail';
}
}else{
echo "File not uploaded successfully.";
}
?>
我究竟做錯了什么?
android function 沒問題; 問題是我必須先將數據庫復制到外部存儲並上傳副本。 PHP 腳本也上傳到我的根目錄而不是我的首選文件夾。 我不得不改變這一行:
$uploads_dir = $_SERVER['DOCUMENT_ROOT'] . '/folder_name/';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.