Android的HTTP文件传输 [英] Android http file transfer

查看:161
本文介绍了Android的HTTP文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Andr​​oid应用程序传输文件到http address.I以下code,但 HHTP的犯规work.The地址写成= 192.168.0.1:8181 用户名=管理密码=空白

我应该在哪里设置的用户名和密码的HTTP文件传输

 字符串URL =htt​​p://192.168.0.1:8181;
文件DIR = Environment.getExternalStorageDirectory();

档案文件=新的文件(目录,DATABASE_NAME);
尝试 {
    HttpClient的HttpClient的=新DefaultHttpClient();

    HttpPost httppost =新HttpPost(URL);

    InputStreamEntity reqEntity =新InputStreamEntity(
            新的FileInputStream(文件),-1);
    reqEntity.setContentType(二进制/八位字节流);
    reqEntity.setChunked(真正的); //如果需要发送多个部分
    httppost.setEntity(reqEntity);
    HTT presponse响应= httpclient.execute(httppost);
    //做些什么回应?
    Log.e(文件传输,完成);
}赶上(例外五){
    //显示错误
    Log.e(错误,e.getMessage());
}
 

解决方案

据最有可能使用基本身份验证的评论。请参阅详情 RFC 1945 ,第11.1节。这意味着形式的报头

 授权:基本<的base64恩codeD-AUTH-信息>
 

必须被添加到该请求。该的base64恩codeD-AUTH-信息是字符串用户名:密码:

 管理​​:空白
 

使用Base64

连接codeD。

总之:

 授权:基本YWRtaW46Ymxhbms =
 

I want to transfer file from my android application to http address.I written following code but doesnt work.The address of hhtp=192.168.0.1:8181 with username=admin and password=blank.

Where should i set username and password for http file transfer

String url = "http://192.168.0.1:8181";
File dir = Environment.getExternalStorageDirectory();

File file = new File(dir,DATABASE_NAME);
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(url);

    InputStreamEntity reqEntity = new InputStreamEntity(
            new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // Send in multiple parts if needed
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    //Do something with response...
    Log.e("file transfer", "done");
} catch (Exception e) {
    // show error
    Log.e("error", e.getMessage());
}

解决方案

According to the comments most likely Basic authentication is used. See RFC 1945, section 11.1 for the details. This means a header of the form

Authorization: Basic <base64-encoded-auth-info>

must be added to the request. The base64-encoded-auth-info is the string username:password:

admin:blank

encoded with base64.

All together:

Authorization: Basic YWRtaW46Ymxhbms=

这篇关于Android的HTTP文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆