0

我正在尝试使用以下代码将几个 SQL 数据库从 Android 上传到在线服务器:

public static void uploadFile(String file,String folder) {

HttpURLConnection conn = null;


String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
    // ------------------ CLIENT REQUEST

    FileInputStream fileInputStream = new FileInputStream(new File(
    path));

    // open a URL connection to the Servlet

    URL url = new URL("http://myserver/upload_file.php");

    // Open a HTTP connection to the URL

    conn = (HttpURLConnection) url.openConnection();

    // Allow Inputs
    conn.setDoInput(true);

    // Allow Outputs
    conn.setDoOutput(true);

    // Don't use a cached copy.
    conn.setUseCaches(false);

    // Use a post method.
    conn.setRequestMethod("POST");

    conn.setRequestProperty("Connection", "Keep-Alive");

    conn.setRequestProperty("Content-Type",
    "multipart/form-data;boundary=" + boundary);

    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
        + path + "" + lineEnd);
    dos.writeBytes(lineEnd);
    // create a buffer of maximum size

    int bytesAvailable = fileInputStream.available();
    int maxBufferSize = 1000;
    // int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte[] buffer = new byte[bytesAvailable];

    // read file and write it into form...

    int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

    while (bytesRead > 0) {
        dos.write(buffer, 0, bytesAvailable);
        bytesAvailable = fileInputStream.available();
        bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
    }

    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // close streams

    fileInputStream.close();
    dos.flush();
    dos.close();

} catch (MalformedURLException ex) {
    Log.e(TAG, "error: " + ex.getMessage(), ex);
} catch (IOException ioe) {
    Log.e(TAG, "error: " + ioe.getMessage(), ioe);
}

try {
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn
    .getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        Log.e(TAG, "Message: " + line);
    }
    rd.close();

} catch (IOException ioex) {
    Log.e(TAG, "error: " + ioex.getMessage(), ioex);
}
return;
}

一切似乎都很顺利,但如果我尝试使用 SQLite Administrator 打开生成的文件,则会引发错误,显示“无法在关闭的数据集上执行此操作”。

有谁知道为什么会发生这种情况?原件和副本的文件大小似乎相同。

更新1:如果尝试访问数据库,这是我的应用程序抛出的错误:

android.database.sqlite.SQLiteDatabaseCorruptException: malformed database schema (?) (code 11): , while compiling: SELECT * FROM Table

任何帮助是极大的赞赏。

谢谢,乔什

4

1 回答 1

0

我发现问题在于我正在上传一个没有添加扩展名的 SQL 数据库文件。在最后添加一个 .DB 后,一切都上传好了。

作为后续,谁能告诉我重命名文件添加数据库,然后在下载后将其剥离是否可行?如果不是,我将不得不撕毁数千行引用数据库名称的代码。

于 2013-06-28T01:18:31.587 回答