Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java从url下载pdf_Java_Pdf - Fatal编程技术网

使用java从url下载pdf

使用java从url下载pdf,java,pdf,Java,Pdf,我正在尝试使用java从url下载pdf。 我使用下面的代码进行相同的操作。我可以下载pdf public class PdfDownloading { public static void DownloadPDFFromURL(String link) { try { URL url = new URL(link); InputStream in = url.openStream();

我正在尝试使用java从url下载pdf。 我使用下面的代码进行相同的操作。我可以下载pdf

public class PdfDownloading 
{
    public static void DownloadPDFFromURL(String link)
    {
        try
        {
            URL url = new URL(link);
            InputStream in = url.openStream();
            FileOutputStream fos = new FileOutputStream(new File("E:\\KOLDump\\charles_hicks\\pdfs\\"+"hello"+".pdf"));
            int length = -1;
            byte[] buffer = new byte[50000];// buffer for portion of data from connection
            while ((length = in.read(buffer)) > -1) {
                fos.write(buffer, 0, length);
            }
            fos.close();
            in.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
    public static void main(String [] args)
    {
        DownloadPDFFromURL("http://www.amjmed.com/article/S0002-9343(06)00676-0/pdf");
    }

}
但问题是我无法使用adobe reader阅读pdf,因为url的内容类型是application/pdf;字符集=utf-8

虽然对于所有内容类型与上面不同的URL,我可以下载和阅读pdf

public class PdfDownloading 
{
    public static void DownloadPDFFromURL(String link)
    {
        try
        {
            URL url = new URL(link);
            InputStream in = url.openStream();
            FileOutputStream fos = new FileOutputStream(new File("E:\\KOLDump\\charles_hicks\\pdfs\\"+"hello"+".pdf"));
            int length = -1;
            byte[] buffer = new byte[50000];// buffer for portion of data from connection
            while ((length = in.read(buffer)) > -1) {
                fos.write(buffer, 0, length);
            }
            fos.close();
            in.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
    public static void main(String [] args)
    {
        DownloadPDFFromURL("http://www.amjmed.com/article/S0002-9343(06)00676-0/pdf");
    }

}

代码中给出了我无法下载pdf的url。

而不是
fos
System.out
中编写代码。你会看到发生了什么。。申请表/pdf;charset=utf-8-附带说明:如果您确实得到了该类型声明,请告诉服务器管理员更正其设置。pdf是二进制格式,所以字符集没有意义。@Ramesh-X:当我打印数据时,我得到的链接表示链接已移动到正在打印的链接。当我在代码中给出该链接并再次打印数据时,它再次表示该链接已移动到另一个链接,当我打开这两个链接时,我被重定向到上面代码中的链接。你
http
链接重定向到
https
链接,据我所知,这是java无法处理的。参考我提供的链接。这个问题是关于将
http
重定向到
https