Android HttpURLConnection模拟浏览器Form上传提交二进制文件

该博客详细介绍了如何在Android中使用HttpURLConnection实现模拟浏览器Form提交,进行二进制文件上传。通过设置请求头和Content-Type,以multipart/form-data格式发送文件,同时传递其他参数。示例代码中包含了文件读取、流处理和错误处理的部分。
摘要由CSDN通过智能技术生成
 /**
     * 上传文件 form post
     * 
     * @category 各种类型,图片,文档等,统一视为file
     * @param url
     * @param request
     * @param params
     * @param filePath
     *            必须
     * @param fileName
     * @param listner
     * @param actionId
     * @param otherFlag
     * @param fileType
     *            必须 1:头像;2:身份证;3:在职证明;4:资产证明;5:名片;
     */
    public static void uploadFile(String uploadUrl, String request, Map<String, String> params,
            String filePath, String fileName, int fileType, HttpConnectionHelperListener listner,
            int actionId, int otherFlag) {

        // form标准提交格式拼接
        String end = "\r\n";
        String twoHyphens = "--"; // 两个连字符
        String boundary = "---------------pedataisfantasticforvcpe"; // 分界符的字符串随便
        try {
            URL url = new URL(uploadUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setRequestMethod("POST");
            // 设置Http请求头
            httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("Charset&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值