0
我想從我的android應用程序上傳一個.db(SQLite)文件到服務器,我想知道我是否可以使用「multipart/form-data」作爲內容類型。對於「application/pdf」的pdf文件,是否有.db文件的特定類型?上傳sqlite .db文件到服務器時使用哪種內容類型?
我想從我的android應用程序上傳一個.db(SQLite)文件到服務器,我想知道我是否可以使用「multipart/form-data」作爲內容類型。對於「application/pdf」的pdf文件,是否有.db文件的特定類型?上傳sqlite .db文件到服務器時使用哪種內容類型?
我實際上只是將「multipart/form-data」作爲內容類型來處理它。當我嘗試'application/x-sqlite3'時,出現錯誤。
這裏是代碼片段我用來張貼.db文件服務器,從這個link借:
public int uploadFile(String sourceFileUri) {
String fileName = sourceFileUri; // the path to my .db file
HttpURLConnection connection = null;
DataOutputStream dataOutputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
Log.e("uploadFile", "Source File not exist ");
return 0;
} else {
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true); // Allow Inputs
connection.setDoOutput(true); //Triggers http POST method.
connection.setUseCaches(false); // Don't use a Cached Copy
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
//conn.setRequestProperty("Content-Type", "application/x-sqlite3; boundary=" + boundary);// when I tried this it didn't work, so you can delete this line
connection.setRequestProperty("uploadedfile", fileName);
dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=" + fileName + "" + lineEnd);
dataOutputStream.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) {
dataOutputStream.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...
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
if (serverResponseCode == 200) {
String msg = "server respose code ";
Log.i(TAG, msg + serverResponseCode);
StringBuilder result = new StringBuilder();
InputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
//JSONObject mResponseJSONObject = new JSONObject(String.valueOf(result)); //convert the respons in json
Log.i(TAG, msg + result);
}
//close the streams //
fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
Log.e(TAG, "MalformedURLException Exception : check script url.");
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Upload file to server Exception : " + e.getMessage(), e);
}
return serverResponseCode;
} // End else block
} //[End uploadFileToServer method]
這是我在服務器端使用PHP腳本:
<?php
if(isset($_FILES)) {
if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "./data/".$_FILES["uploadedfile"]["name"]."_".time())) {
echo " file recieved successfully"
exit;
}
}
echo "Error";
?>
「 multipart/form-data「用於HTML表單。你讀過[Wikipedia](https://en.wikipedia.org/wiki/Internet_media_type)嗎? –
是的,我讀過維基百科頁面,但[RFC](https://www.ietf.org/rfc/rfc2388.txt)聲明它也可以用於非HTML格式,如電子表格,pdf等。 – ctu
但是,您將不得不將文件嵌入到多部分結構中。 –