第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

android中通過轉換Base64實現(xiàn)圖片視屏等文件上傳

標簽:
Android

在android开发中我们经常会实现例如一些图片等流文件的上传。接下来介绍一种转换为Base64 然后通过post的参数形式上传.

 /**
     * 图片文件转Base64字符串 
     * @param path 文件所在的绝对路径加文件名 
     * @return
     */
    private String fileBase64String(String path){
        try {
            FileInputStream fis = new FileInputStream(path);//转换成输入流
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int count = 0;
            while((count = fis.read(buffer)) >= 0){
                baos.write(buffer, 0, count);//读取输入流并写入输出字节流中
            }
            fis.close();//关闭文件输入流
            String uploadBuffer = new String(Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));  //进行Base64编码
            return uploadBuffer;
        } catch (Exception e) {
            return null;
        }

    }

我们首先可以通过此方法把文件转换成String 然后通过Post请求中的Params 传入。
map.put("image",fileBase64String(filePath+fileName));

private String requestPost(String urlPath, String params, OnJsonResponse onJsonResponse){
        String bodyStr = fileBase64String(filePath+fileName);
        byte[] data = bodyStr.getBytes();//获得请求体
        try {
            URL url = new URL(urlPath);  
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setConnectTimeout(timeoutMillis);     //设置连接超时时间
            httpURLConnection.setDoInput(true);                  //打开输入流,以便从服务器获取数据
            httpURLConnection.setDoOutput(true);                 //打开输出流,以便向服务器提交数据
            httpURLConnection.setRequestMethod("POST");     //设置以Post方式提交数据
            httpURLConnection.setUseCaches(false);               //使用Post方式不能使用缓存
            //设置请求体的类型是文本类型
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //设置请求体的长度
            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
            //获得输出流,向服务器写入数据
            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(data);

            int response = httpURLConnection.getResponseCode();            //获得服务器的响应码
            if(response == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = httpURLConnection.getInputStream();
                String result = dealResponseResult(inputStream);
                if(onJsonResponse != null){
                    onJsonResponse.onJsonReceived(urlPath, request_success, result);
                }
                Log.w(TAG,"result = " + result);
                return result;                     //处理服务器的响应结果
            }else{
                if(onJsonResponse != null){
                    onJsonResponse.onJsonReceived(urlPath, request_failure, "fail code : " + response);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            if(onJsonResponse != null){
                onJsonResponse.onJsonReceived(urlPath, request_failure, "fail msg : " + e.getMessage());
            }
        }
        return null;
    }
點擊查看更多內容
12人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質文章

正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消