如何从Android中以jsp结尾的页面正确下载ZIP文件?

问题描述

我曾尝试从如下所示的网址中下载一个zip文件:

https://facturaelectron....download.jsp?folio = A217967 ..... d68f80

当我手动执行此操作时,只需将其粘贴到我的手机浏览器中的任何Web浏览器中,就没有问题,然后它会正确下载zip文件;但是,当我在Android的应用程序中尝试使用它时,它总是会下载损坏的文件,其大小要小于原始文件的大小(500B对应原始文件的84KB);当我尝试在电脑上打开它时,显示格式未知或已损坏。

我尝试了两种不同的方式:

使用DownloadManager

private void beginDownload(String file_link){
    File file = new File(getActivity().getExternalFilesDir("Facturas"),"Factura_corriente.zip");
    DownloadManager.Request request;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        request=new DownloadManager.Request(Uri.parse(file_link))
                .setTitle("Dummy")
                .setDescription("Downloading")
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                .setDestinationUri(Uri.fromFile(file))
                .setMimeType("")
                .setRequiresCharging(false)
                .setAllowedOverMetered(true)
                .setAllowedOverRoaming(true);
        String cookies = CookieManager.getInstance().getCookie(file_link);
        request.addRequestHeader("cookie",cookies);
    }
    else{
        request=new DownloadManager.Request(Uri.parse(file_link))
                .setTitle("Dummy")
                .setDescription("Downloading")
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                .setDestinationUri(Uri.fromFile(file))
                .setAllowedOverRoaming(true);
    }

    DownloadManager downloadManager=(DownloadManager)getActivity().getSystemService(DOWNLOAD_SERVICE);
    downloadId=downloadManager.enqueue(request);
}
private BroadcastReceiver onDownloadComplete=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context,Intent intent) {
        long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
        if(downloadId==id){
            Toast.makeText(getActivity(),"Descarga completada",Toast.LENGTH_SHORT).show();
        }
    }
};

使用HttpURLConnection

private void beginDownload(String file_link){
    try {
        //File file = new File(getActivity().getExternalFilesDir("zip"),"Dummy.zip");
        URL u = new URL(file_link);
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
        FileOutputStream f = new FileOutputStream(new File(getActivity().getExternalFilesDir("zip"),"Dummy.zip"));


        InputStream in = c.getInputStream();

        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ( (len1 = in.read(buffer)) > 0 ) {
            f.write(buffer,len1);
        }
        f.close();
    }catch (Exception e){
        e.printStackTrace();
    }
}
private class DownloadZIP extends AsyncTask<String,Void,String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        pDialog.setCancelable(false);
        pDialog.show();
        pDialog.setContentView(R.layout.custom_loader);
    }

    @Override
    protected String doInBackground(String... params) {
        String res;
        try {
            beginDownload(params[0]);
            res = "001";
        } catch (Exception e) {
            e.printStackTrace();
            res = "-1";
            return res;
        }
        return res;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        if (result.equals("001")) {

        }
        else {

        }
    }
}

如何正确配置zip文件,如何配置我尝试过的两种方法或做一些新的事情?另外,当我使用所示的两种方式中的任何一种时,以.file.zip,... file.jpg,... file.pdf等结尾的url时,它都可以正常工作... 任何好主意吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...