如何使用httpurlconnection將zip文件上傳到遠程服務器?請提供一些URL鏈接,例如代碼... Thnx上傳zip文件到android中的遠程服務器
-2
A
回答
0
你有什麼試過,你面臨什麼問題?上傳zip文件與上傳任何其他文件沒有區別。
有大量的例子,你可以在網上找到。
但是,這又取決於您的遠程服務器如何期待它被上傳。如果你可以用更多的細節更新你的問題,這將有所幫助。
目前,您可以通過這些鏈接。
1
嘗試下面的代碼,並確認這是可行的解決辦法:
StringBuffer responseBody=new StringBuffer();
Log.i(Constants.TAG, "Ready to upload file...");
HttpClient client=new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
Log.i(Constants.TAG, "Set remote URL...");
HttpPost post=new HttpPost("http://IP.IP.IP.IP/file_upload.php");
MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Log.i(Constants.TAG, "Adding file(s)...");
entity.addPart("uploadedfile", new FileBody((FileObj), "application/zip"));
entity.addPart("uploadedfile2", new FileBody((FileObj), "application/zip"));
Log.i(Constants.TAG, "Set entity...");
post.setEntity(entity);
BufferedReader bs=null;
try
{
Log.i(Constants.TAG, "Upload...");
HttpEntity hEntity=client.execute(post).getEntity();
bs=new BufferedReader(new InputStreamReader(hEntity.getContent()));
Log.i(Constants.TAG, "Response length - "+hEntity.getContentLength());
String s="";
while(s!=null)
{
responseBody.append(s);
s=bs.readLine();
Log.i(Constants.TAG, "Response body - "+s);
}
bs.close();
}
catch(IOException ioe)
{
Log.i(Constants.TAG, "Error on getting response from Server, "+ioe.toString());
ioe.printStackTrace();
responseBody.append("...");
}
我的平臺是Android 2.2系統,並使用該解決方案需要得到httpmime.jar的項目庫。
0
1
以下課程對我來說也適用。您可以將設備中的視頻,音頻和任何文件上傳到遠程服務器。
private class Upload extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(ImageUpload.this,
"Please wait...", "Loading...");
}
@Override
protected Void doInBackground(Void... params) {
// Your upload Server SCRIPT
String urlString = "http://192.168.1.176:1001/... // You server URL";
// The file
// The selected path is the location of the file in your device.
File file = new File(selectedPath);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlString);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
// There are more examples above
FileBody fb = new FileBody(file, "audio/3gpp");
if (file.getName().endsWith(".xml")) {
fb = new FileBody(file, "text/xml");
reqEntity.addPart("xml_submission_file", fb);
Log.v("Debug", " file type, adding file: " + file.getName());
} else if (file.getName().endsWith(".jpg")) {
fb = new FileBody(file, "image/jpeg");
reqEntity.addPart(file.getName(), fb);
Log.v("Debug", " file type, adding file: " + file.getName());
} else if (file.getName().endsWith(".3gpp")) {
fb = new FileBody(file, "audio/3gpp");
reqEntity.addPart(file.getName(), fb);
Log.v("Debug", " file type, adding file: " + file.getName());
} else if (file.getName().endsWith(".3gp")) {
fb = new FileBody(file, "video/3gpp");
reqEntity.addPart(file.getName(), fb);
Log.v("Debug", " file type, adding file: " + file.getName());
} else if (file.getName().endsWith(".mp4")) {
fb = new FileBody(file, "video/mp4");
reqEntity.addPart(file.getName(), fb);
Log.v("Debug", " file type, adding file: " + file.getName());
} else {
Log.w("Debug", "unsupported file type, not adding file: "
+ file.getName());
}
FormBodyPart bodyPart = new FormBodyPart("uploadedfile", fb);
reqEntity.addPart(bodyPart);
httppost.setEntity(reqEntity);
try {
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder mUploadResponse = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
mUploadResponse = mUploadResponse.append(sResponse);
}
JSONObject mUploadResponseObject = new JSONObject(
mUploadResponse.toString());
mUploadResponseObject.getJSONArray("response");
try {
JSONArray jsonArray = mUploadResponseObject
.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++) {
uploadStatus = jsonArray.getJSONObject(i)
.getJSONObject("send").getString("message");
uploadPhotoID = jsonArray.getJSONObject(i)
.getJSONObject("send").getString("id");
}
} catch (Exception e) {
Log.d("DEBUG",
"The Json response message : " + e.getMessage());
}
} catch (ClientProtocolException e) {
Log.d("DEBUG",
"The server ClientProtocolException response message : "
+ e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.d("DEBUG", "The server IOException response message : "
+ e.getMessage());
e.printStackTrace();
} catch (JSONException e) {
Log.d("DEBUG", "The JSONException server response message : "
+ e.getMessage());
e.printStackTrace();
}
return null;
}
}
protected void onPostExecute(Void result) {
if (mProgressDialog.isShowing() && mProgressDialog != null) {
mProgressDialog.dismiss();
}
mHandler.sendEmptyMessage(0);
}
它適合我。嘗試一下。
相關問題
- 1. 使用php curl將zip文件上傳到遠程服務器
- 2. 從Android設備上傳文件到遠程服務器
- 3. cURL將文件上傳到MS Windows上的遠程服務器
- 4. 將文件上傳到遠程服務器的好處
- 5. 將上傳的文件移動到遠程服務器
- 6. 將文件上傳到遠程服務器的sailsJS
- 7. 上傳到遠程服務器
- 8. 在Android上傳文件到服務器
- 9. Android上傳文件到服務器
- 10. Android文件上傳到服務器
- 11. 在Android上傳文件到服務器
- 12. maven添加zip文件部署到遠程服務器
- 13. 使用PHP將文件從遠程服務器上傳到FTP服務器
- 14. 使用PHP將多個文件上傳到遠程服務器?
- 15. 上傳文件到遠程服務器時出錯(Windows,cURL)
- 16. 將文件上傳到遠程服務器(PHP)
- 17. 從節點上傳文件到遠程服務器
- 18. 無法上傳文件到遠程FTP服務器
- 19. 將文件上傳到遠程服務器
- 20. 使用ASP.Net(VB)將文件上傳到遠程服務器1.1
- 21. 將文件上傳到遠程服務器
- 22. 上傳MP3音頻文件到遠程服務器
- 23. 使用ajax將文件上傳到遠程服務器
- 24. 使用python下載/上傳文件到遠程windows服務器
- 25. 將文件上傳到遠程服務器
- 26. 將文件上傳到遠程服務器,我應該如何?
- 27. 無法上傳遠程服務器上的文件
- 28. 多臺遠程上傳服務器到服務器
- 29. 如何將java服務器上傳到遠程服務器?
- 30. Zip文件上傳到服務器時被破壞