我正在尝试使用以下代码将多个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
非常感谢任何帮助。
谢谢, 约什
答案 0 :(得分:0)
我发现问题是我上传了一个SQL数据库文件而没有添加扩展名。将.DB添加到最后所有上传的井后。
作为后续,任何人都可以告诉我重命名添加数据库的文件,然后在下载后将其剥离后工作吗?如果不是,我将不得不撕掉引用数据库名称的数千行代码。