在android中将zip文件上载到远程服务器

在android中将zip文件上载到远程服务器,android,zip,httpurlconnection,Android,Zip,Httpurlconnection,如何使用httpurlconnection将zip文件上载到远程服务器?请给我提供一些url链接,例如代码…Thnx您尝试了什么,您面临什么问题?上载zip文件与上载任何其他文件没有什么不同 你可以在网上找到大量的例子 但同样,这取决于远程服务器希望如何上传。如果你能用更多的细节更新你的问题,那会有帮助的 目前,您可以浏览这些链接 尝试以下代码并确认这是可行的解决方案: StringBuffer responseBody=new StringBuffer(); Log.i(Constants.

如何使用httpurlconnection将zip文件上载到远程服务器?请给我提供一些url链接,例如代码…Thnx

您尝试了什么,您面临什么问题?上载zip文件与上载任何其他文件没有什么不同

你可以在网上找到大量的例子

但同样,这取决于远程服务器希望如何上传。如果你能用更多的细节更新你的问题,那会有帮助的

目前,您可以浏览这些链接


尝试以下代码并确认这是可行的解决方案:

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,使用此解决方案需要将httpime.jar作为项目库。

以下类也适用于我。您可以将设备中的视频、音频和任何文件上载到远程服务器

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);
}
私有类上载扩展异步任务{
受保护的void onPreExecute(){
mProgressDialog=ProgressDialog.show(ImageUpload.this,
“请稍候…”,“正在加载…”);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//您的上载服务器脚本
字符串URL字符串=”http://192.168.1.176:1001/...  //您可以使用“服务器URL”;
//档案
//所选路径是文件在设备中的位置。
文件文件=新文件(selectedPath);
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(urlString);
多方实体REQUENTITY=新多方实体(
HttpMultipartMode.BROWSER_兼容);
//上面还有更多的例子
FileBody fb=新的FileBody(文件,“音频/3gpp”);
if(file.getName().endsWith(“.xml”)){
fb=新的文件体(文件,“text/xml”);
reqEntity.addPart(“xml提交文件”,fb);
Log.v(“调试”,“文件类型,添加文件:”+file.getName());
}else if(file.getName().endsWith(“.jpg”)){
fb=新的文件体(文件,“图像/jpeg”);
reqEntity.addPart(文件.getName(),fb);
Log.v(“调试”,“文件类型,添加文件:”+file.getName());
}else if(file.getName().endsWith(“.3gpp”)){
fb=新的文件体(文件“音频/3gpp”);
reqEntity.addPart(文件.getName(),fb);
Log.v(“调试”,“文件类型,添加文件:”+file.getName());
}else if(file.getName().endsWith(“.3gp”)){
fb=新的文件体(文件,“视频/3gpp”);
reqEntity.addPart(文件.getName(),fb);
Log.v(“调试”,“文件类型,添加文件:”+file.getName());
}else if(file.getName().endsWith(“.mp4”)){
fb=新的文件体(文件“视频/mp4”);
reqEntity.addPart(文件.getName(),fb);
Log.v(“调试”,“文件类型,添加文件:”+file.getName());
}否则{
Log.w(“调试”,“不支持的文件类型,不添加文件:”
+getName());
}
FormBodyPart bodyPart=新的FormBodyPart(“上传文件”,fb);
要求实体添加部件(车身部件);
httppost.setEntity(reqEntity);
试一试{
HttpResponse response=httpclient.execute(httppost);
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(
response.getEntity().getContent(),“UTF-8”);
字符串响应;
StringBuilder mUploadResponse=新StringBuilder();
而((sResponse=reader.readLine())!=null){
mUploadResponse=mUploadResponse.append(sResponse);
}
JSONObject mUploadResponseObject=新JSONObject(
mUploadResponse.toString());
getJSONArray(“响应”);
试一试{
JSONArray JSONArray=mUploadResponseObject
.getJSONArray(“回应”);
for(int i=0;i
它对我很有效。试试这个