@Override public void onDownloadStart(final String url, final String userAgent, final String contentDisposition, final String mimeType, final long contentLength) { final String suggestedFilename = URLUtil.guessFileName(url, contentDisposition, mimeType); if (mListener != null) { mListener.onDownloadRequested(url, suggestedFilename, mimeType, contentLength, contentDisposition, userAgent); } }
protected String doInBackground(String... urls) { URL url; String filename = null; try { url = new URL(urls[0]); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); conn.setInstanceFollowRedirects(false); String depo = conn.getHeaderField("Content-Disposition"); if (depo != null) filename = depo.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1"); else filename = URLUtil.guessFileName(urls[0], null, null); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { } return filename; }
@Override public void onDownloadStart(final String url, final String userAgent, final String contentDisposition, final String mimeType, final long contentLength) { final String suggestedFilename = URLUtil.guessFileName(url, contentDisposition, mimeType); if (mListener != null) { mListener.onDownloadRequested(url, suggestedFilename, mimeType, contentLength, contentDisposition, userAgent); } }
public void downloadFile() { URL url = null; String file = ""; try { //url = new URL ("http://www.cs.uwyo.edu/~seker/courses/4730/example/pic.jpg"); //for a bigger file: http://www.nasa.gov/images/content/206402main_jsc2007e113280_hires.jpg //for reason I don't understand, I can't download it from nasa on the phone... weird... // url = new URL("http://www.nasa.gov/images/content/206402main_jsc2007e113280_hires.jpg"); url = new URL ("http://www.cs.uwyo.edu/~seker/courses/4730/example/206402main_jsc2007e113280_hires.jpg"); //yes, I could write the file name, but this means I don't have think it about if I change files. file = URLUtil.guessFileName(url.getFile(), null, null); } catch (MalformedURLException e) { e.printStackTrace(); } //setup intent and send it. Intent getfile = new Intent(getActivity().getBaseContext(), fileDlService.class); getfile.putExtra("FILE", file); getfile.putExtra("URL", url); getActivity().startService(getfile); }
String cookies = CookieManager.getInstance() .getCookie(url); String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
LCTX.d(Thread.currentThread().getName() + ": Content-Length: " + contentLength); final String guessFileName = URLUtil.guessFileName(uri.get().toString(), contentDisposition.replace("attachement", "attachment"), mimeType);
@Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { try { CookieManager cookieManager = CookieManager.getInstance(); URL url2 = new URL(url); String cookie = cookieManager.getCookie(url2.getHost()); Request request = new Request(Uri.parse(url)); request.addRequestHeader("Cookie", cookie); request.setMimeType(mimetype); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); new File(Environment.getExternalStorageDirectory() + "/Download/pkuhelper/").mkdirs(); File file = new File(Environment.getExternalStorageDirectory() + "/Download/pkuhelper/" + filename); if (file.exists()) file.delete(); request.setDestinationUri(Uri.fromFile(file)); request.setTitle("正在下载" + filename + "..."); request.setDescription("文件保存在" + file.getAbsolutePath()); DownloadManager downloadManager = (DownloadManager) subActivity.getSystemService(Context.DOWNLOAD_SERVICE); downloadManager.enqueue(request); CustomToast.showInfoToast(subActivity, "文件下载中,请在通知栏查看进度"); } catch (Exception e) { subActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } } });
@Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { try { CookieManager cookieManager = CookieManager.getInstance(); URL url2 = new URL(url); String cookie = cookieManager.getCookie(url2.getHost()); Request request = new Request(Uri.parse(url)); request.addRequestHeader("Cookie", cookie); request.setMimeType(mimetype); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); new File(Environment.getExternalStorageDirectory() + "/Download/pkuhelper/").mkdirs(); File file = new File(Environment.getExternalStorageDirectory() + "/Download/pkuhelper/" + filename); if (file.exists()) file.delete(); request.setDestinationUri(Uri.fromFile(file)); //request.setDestinationUri(uri) request.setTitle("正在下载" + filename + "..."); request.setDescription("文件保存在" + file.getAbsolutePath()); DownloadManager downloadManager = (DownloadManager) subActivity.getSystemService(Context.DOWNLOAD_SERVICE); downloadManager.enqueue(request); CustomToast.showInfoToast(subActivity, "文件下载中,请在通知栏查看进度"); } catch (Exception e) { subActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } } });
String fileName = URLUtil.guessFileName( url, null, OMADownloadHandler.OMA_DRM_MESSAGE_MIME); final DownloadInfo downloadInfo =
@Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) { if(!check_permission(2)){ ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, file_perm); }else { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setMimeType(mimeType); String cookies = CookieManager.getInstance().getCookie(url); request.addRequestHeader("cookie", cookies); request.addRequestHeader("User-Agent", userAgent); request.setDescription(getString(R.string.dl_downloading)); request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType)); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType)); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); assert dm != null; dm.enqueue(request); Toast.makeText(getApplicationContext(), getString(R.string.dl_downloading2), Toast.LENGTH_LONG).show(); } } });
/** * * @param context context * @param video video */ // TODO, offer which version to download public static void Download(Context context, Video video) { String url = video.getFiles().get(0).getFileDownloadUrl(); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setDescription(video.getDescription()); request.setTitle(video.getName()); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, null, null)); // get download service and enqueue file DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request); } }
String desc = getResources().getString(R.string.downloading, getLastPathOfUri(url)); request.setDescription(desc); request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype)); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype)); dm.enqueue(request); if (proClient !=null)
public void downloadFileAsk(String url, String contentDisposition, String mimeType) { String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType); if (!hasStoragePermission()) { mWaitingDownloadUrl = url; requestStoragePermission(); return; } mWaitingDownloadUrl = null; new AlertDialog.Builder(this) .setTitle(R.string.download_title) .setMessage(getString(R.string.download_message, fileName)) .setPositiveButton(getString(R.string.download_positive), (dialog, which) -> fetchFile(url, fileName)) .setNegativeButton(getString(R.string.dismiss), ((dialog, which) -> dialog.dismiss())) .show(); }
String url = omaInfo.getValue(OMA_OBJECT_URI); if (TextUtils.isEmpty(fileName)) { fileName = URLUtil.guessFileName(url, null, mimeType);