如何解决如何从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 举报,一经查实,本站将立刻删除。