2013-06-24 37 views
0

我嘗試使用下面的代碼來自Android的幾個SQL數據庫上傳到網上的服務器:SQL數據庫損壞的上傳後(安卓)

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管理員它拋出一個錯誤閱讀「不能在一個封閉的數據集上執行此操作」。

有沒有人有一個想法,爲什麼這可能會發生?原件和副本的文件大小看起來是相同的。

更新1: 這裏,如果試圖訪問該數據庫通過我的應用程序拋出的錯誤:

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

任何幫助是極大的讚賞。

感謝, 喬希

+0

您使用SQL Server 2008嗎? –

+0

如果您使用的是SQL Server 2008,那麼您可能未將'setAppRole'應用於數據庫。這可能會導致這個問題。請參閱:http://stackoverflow.com/questions/13288084/cannot-perform-this-operation-on-a-closed-dataset –

+0

我沒有在線使用SQL服務器。我只備份要在其他用戶的設備上本地共享和使用的文件。 – Josh

回答

0

我發現,問題是,我沒有上傳和延伸加入到它的SQL數據庫文件。在添加一個.DB到所有上傳完成後。

作爲一個後續,任何人都可以告訴我重命名文件添加數據庫,然後在下載後將其剝離下來將工作?如果沒有,我將不得不撕毀數千行引用數據庫名稱的代碼。