2013-06-24 41 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到所有上传完成后。

作为一个后续,任何人都可以告诉我重命名文件添加数据库,然后在下载后将其剥离下来将工作?如果没有,我将不得不撕毁数千行引用数据库名称的代码。