java文檔下載,Java 文檔

本文目錄一覽:

如何在官網下載Java的API文檔?

題主你好,

首先導航到jdk下載的首頁:

2.根據自己使用的jdk版本, 選擇上圖紅框中相應的超鏈接, 點進去.

假設我使的是jdk15, 點進去後看到的頁面為:

3.點擊上圖中紅框中的Download the jdk(文檔和jdk安裝包都在這裏面),看到頁面為:

4.點擊上圖中紅框選中的Documentation Download, 看到頁面為:

5.點擊上圖紅框中的超鏈接,會有彈窗:

6.按上圖先將第1個紅框勾選上, 然後點擊Download jdk-16-doc-all.zip.

7.第6步做完就會自己下載了, 下載的是一個後綴為zip的安裝包. 解壓後, 導航到api目錄, 裏面有一個index.html,在瀏覽器裏面訪問這個index.html就可以用了.

*.如果題主有圖形界面的話, 直接雙擊api步錄下的index.html就可以.

—–

希望可以幫到題主, 歡迎追問.

如何用Java下載網上的文件?

這個涉及到文件的映射與跳轉了,想要用JAVA寫的話,最起碼你傳給它的應該是實質性的直接鏈接,這樣它才可以建立連接,通過輸入輸出流。當然,如果你夠厲害的話,也可以自己通過程序先對網址進行自動性的處理,然後分析出實質性的文件可靠地址,就可以下載了。

我看了一下,你的這個地址不是直接的鏈接,你發下載請求的時候人家直接在後台導向目標文件了。

用java實現文件的下載,如何提高下載速度(非web開發)

下面貼出的代碼是一個簡單的讀取遠程文件保存到本地的實現,至於提高下載速度你可以利用多線程,具體可參考最下面的那個網址——

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

public class DownloadTester {

public static void main(String[] args) throws IOException {

String urlStr = “”;

String path = “D:/”;

String name = urlStr.substring(urlStr.trim().lastIndexOf(“/”));

URL url = new URL(urlStr);

InputStream in = url.openConnection().getInputStream();

File file = new File(path + name);

FileOutputStream out = new FileOutputStream(file, true);

int counter = 0;

int ch;

byte[] buffer = new byte[1024];

while ((ch = in.read(buffer)) != -1) {

out.write(buffer, 0, ch);

counter += ch;

System.out.println(counter + “:byte”);

}

out.flush();

in.close();

out.close();

}

}

Java 下載文件的方法怎麼寫

參考下面

public HttpServletResponse download(String path, HttpServletResponse response) {

try {

// path是指欲下載的文件的路徑。

File file = new File(path);

// 取得文件名。

String filename = file.getName();

// 取得文件的後綴名。

String ext = filename.substring(filename.lastIndexOf(“.”) + 1).toUpperCase();

// 以流的形式下載文件。

InputStream fis = new BufferedInputStream(new FileInputStream(path));

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();

// 清空response

response.reset();

// 設置response的Header

response.addHeader(“Content-Disposition”, “attachment;filename=” + new String(filename.getBytes()));

response.addHeader(“Content-Length”, “” + file.length());

OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

response.setContentType(“application/octet-stream”);

toClient.write(buffer);

toClient.flush();

toClient.close();

} catch (IOException ex) {

ex.printStackTrace();

}

return response;

}

// 下載本地文件

public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {

String fileName = “Operator.doc”.toString(); // 文件的默認保存名

// 讀到流中

InputStream inStream = new FileInputStream(“c:/Operator.doc”);// 文件的存放路徑

// 設置輸出的格式

response.reset();

response.setContentType(“bin”);

response.addHeader(“Content-Disposition”, “attachment; filename=\”” + fileName + “\””);

// 循環取出流中的數據

byte[] b = new byte[100];

int len;

try {

while ((len = inStream.read(b)) 0)

response.getOutputStream().write(b, 0, len);

inStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

// 下載網絡文件

public void downloadNet(HttpServletResponse response) throws MalformedURLException {

int bytesum = 0;

int byteread = 0;

URL url = new URL(“windine.blogdriver.com/logo.gif”);

try {

URLConnection conn = url.openConnection();

InputStream inStream = conn.getInputStream();

FileOutputStream fs = new FileOutputStream(“c:/abc.gif”);

byte[] buffer = new byte[1204];

int length;

while ((byteread = inStream.read(buffer)) != -1) {

bytesum += byteread;

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

//支持在線打開文件的一種方式

public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {

File f = new File(filePath);

if (!f.exists()) {

response.sendError(404, “File not found!”);

return;

}

BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));

byte[] buf = new byte[1024];

int len = 0;

response.reset(); // 非常重要

if (isOnLine) { // 在線打開方式

URL u = new URL(“” + filePath);

response.setContentType(u.openConnection().getContentType());

response.setHeader(“Content-Disposition”, “inline; filename=” + f.getName());

// 文件名應該編碼成UTF-8

} else { // 純下載方式

response.setContentType(“application/x-msdownload”);

response.setHeader(“Content-Disposition”, “attachment; filename=” + f.getName());

}

OutputStream out = response.getOutputStream();

while ((len = br.read(buf)) 0)

out.write(buf, 0, len);

br.close();

out.close();

}

Java文件下載怎麼實現的

下載就很簡單了

把你要下載的文件做成超級鏈接,可以不用任何組件

比如說

下載一個word文檔

a href=”名稱.doc”名稱.doc/a

路徑你自己寫

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.ProtocolException;

import java.net.URI;

import java.net.URL;

import java.util.Random;

/**

*

* 實現了下載的功能*/

public class SimpleTh {

public static void main(String[] args){

// TODO Auto-generated method stub

//String path = “倩女幽魂.mp3”;//MP3下載的地址

String path =””;

try {

new SimpleTh().download(path, 3); //對象調用下載的方法

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static String getFilename(String path){//獲得文件的名字

return path.substring(path.lastIndexOf(‘/’)+1);

}

public void download(String path,int threadsize) throws Exception//下載的方法

{//參數 下載地址,線程數量

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();//獲取HttpURLConnection對象

conn.setRequestMethod(“GET”);//設置請求格式,這裡是GET格式

conn.setReadTimeout(5*1000);//

int filelength = conn.getContentLength();//獲取要下載文件的長度

String filename = getFilename(path);

File saveFile = new File(filename);

RandomAccessFile accessFile = new RandomAccessFile(saveFile, “rwd”);

accessFile.setLength(filelength);

accessFile.close();

int block = filelength%threadsize ==0?filelength/threadsize:filelength/threadsize+1;

for(int threadid = 0;threadid=threadsize;threadid++){

new DownloadThread(url,saveFile,block,threadid).start();

}

}

private final class DownloadThread extends Thread{

private URL url;

private File saveFile;

private int block;//每條線程下載的長度

private int threadid;//線程id

public DownloadThread(URL url,File saveFile,int block,int threadid){

this.url = url;

this.saveFile= saveFile;

this.block = block;

this.threadid = threadid;

}

@Override

public void run() {

//計算開始位置的公式:線程id*每條線程下載的數據長度=?

//計算結束位置的公式:(線程id+1)*每條線程下載數據長度-1=?

int startposition = threadid*block;

int endposition = (threadid+1)*block-1;

try {

try {

RandomAccessFile accessFile = new RandomAccessFile(saveFile, “rwd”);

accessFile.seek(startposition);//設置從什麼位置寫入數據

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestMethod(“GET”);

conn.setReadTimeout(5*1000);

conn.setRequestProperty(“Range”,”bytes= “+startposition+”-“+endposition);

InputStream inStream = conn.getInputStream();

byte[]buffer = new byte[1024];

int len = 0;

while((len = inStream.read(buffer))!=-1){

accessFile.write(buffer, 0, len);

}

inStream.close();

accessFile.close();

System.out.println(“線程id:”+threadid+”下載完成”);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

原創文章,作者:RJLK,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/142942.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
RJLK的頭像RJLK
上一篇 2024-10-14 18:44
下一篇 2024-10-14 18:44

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29
  • Java 8 Group By 會影響排序嗎?

    是的,Java 8中的Group By會對排序產生影響。本文將從多個方面探討Group By對排序的影響。 一、Group By的概述 Group By是SQL中的一種常見操作,它…

    編程 2025-04-29

發表回復

登錄後才能評論