Android下载多部分格式的文件(图像/ txt)

时间:2018-08-10 00:01:07

标签: java android

我正在尝试使用API​​从服务器下载图像。我收到的响应是多部分格式,其中第一部分是我收到的任何错误代码,第二部分是二进制格式的文件。 --kMvjP0H6CUgbINPNnGJg7JUvkwz4QEm_Content-Disposition:表格数据; name =“ errorcode” Content-Type:文本/纯文本; charset = US-ASCIIContent-Transfer-Encoding:8bit0--kMvjP0H6CUgbINPNnGJg7JUvkwz4QEm_Content-Disposition:form-data; name =“ Value-1.txt”; filename =“ Value-1.txt” Content-Type:application / octet-stream; charset = ISO-8859-1Content-Transfer-Encoding:二进制

âPNG

IHDR8Ä+aÁ  IDATxúÏ› w | \Á}Á˚ÔÉfi{%∞∞¡II™V≥öe; ä‚v◊I÷±77…çs„îç≥Ø≥Òn≤õõl÷éù8Ú∫»í,ÀIEe5±〜 ê`AÔΩ --kMvjP0H6CUgbINPNnGJg7JUvkwz4QEm _--

我可以访问结果并获取二进制数据,但是无法将其处理为imageView或将其保存到图库中。

我尝试使用Bitmap通过将整个流放在ByteArrayOutputStream中来对其进行处理,但仍然无法处理。

下面是我正在使用的异步任务

尝试{

        String download_url = weburl + "downloadDocument.do";


        URL url = new URL(download_url);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        urlConn.setRequestMethod("POST");
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);

        OutputStream outputStream = urlConn.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));



        String post_data = URLEncoder.encode("sessionid", "UTF-8") + "=" + URLEncoder.encode(sessionid, "UTF-8") +"&"+
                URLEncoder.encode("exportid", "UTF-8") + "=" + URLEncoder.encode(docid, "UTF-8");

        Log.d("URL data is", download_url+"?"+post_data);

        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();

        outputStream.close();

        InputStream inputStream = urlConn.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
        BufferedInputStream bis = new BufferedInputStream(inputStream);

        String result = "";
        String line;
        int count = 0;

        while((line = bufferedReader.readLine()) != null) {



            if(count >= 12) {
                Log.d("Line is: ", line);
                result += line;
            }

            count++;

        }

        Log.d("Result before", result);


        byte[] bytes = new byte[1024];

        byte[] inputBytes = result.getBytes();

        while (inputStream.read(bytes) != -1){
            baos.write(bytes);
        }

        bufferedReader.close();
        inputStream.close();



        urlConn.disconnect();



      return baos.toByteArray();


    }   catch (Exception e){
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onPostExecute(byte[] bytes) {

// protected void onPostExecute(String result){

    Bitmap img = BitmapFactory.decodeByteArray(bytes, 0 , bytes.length);
    Log.d("l", String.valueOf(bytes.length));
    imageView.setImageBitmap(img);


    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root);
    myDir.mkdirs();
    String fname = "Image.jpg";
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    Log.i("LOAD-IMAGRE", root + fname);
    try {
        FileOutputStream out = new FileOutputStream(file);
        img.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }



}

0 个答案:

没有答案