當前位置:編程學習大全網 - 編程語言 - Java程序pdf文件

Java程序pdf文件

主要是URL和HttpURLConnection類的使用,參見代碼:

進口?Java . io . data inputstream;

進口?Java . io . file output stream;

進口?Java . io . io exception;

進口?Java . net . httpurl connection;

進口?Java . net . URL;

公共?班級?HttpDownloader?{

私人?靜電?決賽?字符串?遠程文件URL?=?“http://211.103.156.163/u/CMS/www/201511/25051940 i6ou . pdf”;

私人?靜電?決賽?字符串?本地文件路徑?=?”D:/some . pdf”;?//?妳把它存起來了?文件的路徑

公共?靜電?作廢?main(String[]?args)?{

新的?HttpDownloader(遠程文件URL,?本地文件路徑)。下載();

}

私人?字符串?remoteFileUrl

私人?字符串?localFilePath

公共?HttpDownloader(字符串?remoteFileUrl,?字符串?localFilePath)?{

this.remoteFileUrl?=?remoteFileUrl

this.localFilePath?=?localFilePath

}

公共?作廢?下載()?{

試試?{

網址?網址?=?新的?URL(remoteFileUrl);

HttpURLConnection?httpURLConnection?=?(HttpURLConnection)?URL . open connection();

httpurlconnection . setconnecttimeout(5?*?1000);?//?5000?毫秒內沒有連接?放棄連接。

httpurl connection . connect();?//?連接

System.out.println("連接?網址?成功~);

int?文件長度?=?httpurlconnection . get contentlength();

System.out.println("文件大小:"?+?(fileLenght?/?1024.0)?+?"?KB”);

System.out.println("開始下載...");

試試?(DataInputStream?dis?=?新的?data inputstream(httpurlconnection . getinputstream());

FileOutputStream?fos?=?新的?FileOutputStream(localFilePath))?{

byte[]?buf?=?新的?byte[10240];?//?根據實際情況?增加?buf?大小

為了什麽?(int?readSize?(readSize?=?dis.read(buf))?& gt?0;)?{

fos.write(buf,?0,?readSize);

}

System.out.println("下載完成~ ");

}?接住?(IOException?ex)?{

System.out.println("下載出錯");

}

httpurl connection . disconnect();

}?接住?(IOException?ex)?{

System.out.println("URL?不存在或連接超時”);

}

}

}

  • 上一篇:大學看哪些書比較合適?
  • 下一篇:在電路模塊中同壹個頻率的晶振貼片和插件是壹樣用的嗎?
  • copyright 2024編程學習大全網