[英]Problem with setting header “Content-Type” in uploading file with HttpClient4
[英]Which content-type to use when Uploading sqlite .db file to a server?
我想將android應用程序中的.db(SQLite)文件上傳到服務器,我想知道是否可以使用“ multipart / form-data”作為內容類型。 .db文件和pdf文件是否有任何特定類型,即“ application / pdf”?
實際上,我只使用“ multipart / form-data”作為內容類型。 當我嘗試使用“ application / x-sqlite3”時,出現錯誤。
這是我從該鏈接借來的用於將.db文件發布到服務器的代碼片段:
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";
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.