【问题标题】:How to download a pdf file from a php powered website with URL of kind http://myurl.com/file.php/...../requireddownload.pdf如何从带有 http://myurl.com/file.php/...../requireddownload.pdf 的 URL 的 php 驱动的网站下载 pdf 文件
【发布时间】:2017-05-04 02:04:33
【问题描述】:

我尝试了很多方法,例如使用异步任务和默认下载管理器,但是,我以下载文件结束(我相信它正在下载 file.php 而不是 requireddownload.pdf 文件),这不是必需的文件。但是下载文件的名称和扩展名是requrieddownload.pdf。

提前谢谢你。

【问题讨论】:

    标签: android android-studio webview android-webview android-webservice


    【解决方案1】:

    在 AndroidManifest.xml 中声明权限的第一步

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    

    创建一个下载器类

    public class Downloader {
    
        public static void DownloadFile(String fileURL, File directory) {
            try {
    
                FileOutputStream f = new FileOutputStream(directory);
                URL u = new URL(fileURL);
                HttpURLConnection c = (HttpURLConnection) u.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();
    
                InputStream in = c.getInputStream();
    
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = in.read(buffer)) > 0) {
                    f.write(buffer, 0, len1);
                }
                f.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    

    最后创建一个从互联网下载 PDF 文件的活动,

    public class PDFFromServerActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            String extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();
            File folder = new File(extStorageDirectory, "pdf");
            folder.mkdir();
            File file = new File(folder, "Read.pdf");
            try {
                file.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            Downloader.DownloadFile("http://www.nmu.ac.in/ejournals/aspx/courselist.pdf", file);
    
            showPdf();
        }
        public void showPdf()
            {
                File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
                PackageManager packageManager = getPackageManager();
                Intent testIntent = new Intent(Intent.ACTION_VIEW);
                testIntent.setType("application/pdf");
                List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                Uri uri = Uri.fromFile(file);
                intent.setDataAndType(uri, "application/pdf");
                startActivity(intent);
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多