java下載url代碼 java下載路徑

java文件下載接口沒有url

如果Java文件下載接口沒有URL,可以使用一些其他方法來實現文件下載功能,比如使用Java代碼讀取文件數據,然后將數據寫入新文件中。也可以使用HTTP服務器,通過編寫特定的Servlet或JSP代碼,創(chuàng)建HTTP請求,然后處理和響應請求,從而實現文件下載功能。

創(chuàng)新互聯專注于赫山網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供赫山營銷型網站建設,赫山網站制作、赫山網頁設計、赫山網站官網定制、小程序定制開發(fā)服務,打造赫山網絡公司原創(chuàng)品牌,更為您提供赫山網站排名全網營銷落地服務。

我用java做了一個通過url地址下載指定文件的功能,文件名可能包含中文,IE正常,火狐失敗.

您好!很高興為您答疑!

火狐下您可以安裝Firebug檢查頁面代碼,它集HTML查看和編輯、Javascript控制臺、網絡狀況監(jiān)視器于一體,是開發(fā)JavaScript、CSS、HTML和Ajax的得力助手。

您可以在火狐社區(qū)了解更多內容。希望我的回答對您有所幫助,如有疑問,歡迎繼續(xù)在本平臺咨詢。

Java如何利用url下載MP3保存到本地?

Java如何利用url下載MP3保存的方法:

1 /** ;

2 ?? ? * TODO 下載文件到本地 ;

3 ? ? ?* @author nadim ?;

4 ?? ? * @date Sep 11, 2015 11:45:31 AM ;

5 ?? ? * @param fileUrl 遠程地址 ;

6 ?? ? * @param fileLocal 本地路徑 ;

7 ?? ? * @throws Exception ;

8 ? ? ?*/ ;

9 ? ? public void downloadFile(String fileUrl,String fileLocal) throws Exception {;

10 ? ? ? ? URL url = new URL(fileUrl);

11 ? ? ? ? HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();

12 ? ? ? ? urlCon.setConnectTimeout(6000);

13 ? ? ? ? urlCon.setReadTimeout(6000);

14 ? ? ? ? int code = urlCon.getResponseCode();

15 ? ? ? ? if (code != HttpURLConnection.HTTP_OK) {

16 ? ? ? ? ? ? throw new Exception("文件讀取失敗");

17 ?? ? ? ?} ? ? ?

18 ? ? ? ? //讀文件流;

19 ? ? ? ?DataInputStream in = new DataInputStream(urlCon.getInputStream());

20 ? ? ? ? DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));

21 ? ? ? ? byte[] buffer = new byte[2048];

22 ? ? ? ? int count = 0;

23 ? ? ? ? while ((count = in.read(buffer)) 0) {;

24 ? ? ? ? ? ? out.write(buffer, 0, count);

25 ?? ? ? ?}

26 ?? ? ? ?out.close();

27 ?? ? ? ?in.close();

28 ? ? }。

Java是一門面向對象編程語言,不僅吸收了C++語言的各種優(yōu)點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特征。

Java語言作為靜態(tài)面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優(yōu)雅的思維方式進行復雜的編程 。

設計一個JAVA程序,下載由URL指定的網頁的源代碼,找出其中所有的超鏈接。

import?java.awt.BorderLayout;

import?java.awt.event.ActionEvent;

import?java.awt.event.ActionListener;

import?java.io.BufferedReader;

import?java.io.IOException;

import?java.io.InputStream;

import?java.io.InputStreamReader;

import?java.net.HttpURLConnection;

import?java.net.MalformedURLException;

import?java.net.URL;

import?java.util.regex.Matcher;

import?java.util.regex.Pattern;

import?javax.swing.JFrame;

import?javax.swing.JLabel;

import?javax.swing.JPanel;

import?javax.swing.JScrollPane;

import?javax.swing.JTextArea;

import?javax.swing.JTextField;

public?class?HttpViewer?extends?JFrame?{

private?JTextField?urlInput;

private?JTextArea?viewArea;

public?static?void?main(String[]?args)?{

new?HttpViewer();

}

public?HttpViewer()?{

this.setTitle("Http?Viewer");

this.setSize(800,?600);

this.setResizable(false);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

initPanel();

initAction();

this.setVisible(true);

}

//?這個方法用來設置窗口布局

private?void?initPanel()?{

JPanel?northPanel?=?new?JPanel();

JLabel?urlInputLabel?=?new?JLabel("URL:");

urlInput?=?new?JTextField(60);

northPanel.add(urlInputLabel);

northPanel.add(urlInput);

this.add(northPanel,?BorderLayout.NORTH);

JPanel?centerPanel?=?new?JPanel();

viewArea?=?new?JTextArea(27,?60);

centerPanel.add(new?JScrollPane(viewArea));

this.add(centerPanel);

}

//?這個方法用來設置事件

private?void?initAction()?{

urlInput.addActionListener(new?ActionListener()?{

public?void?actionPerformed(ActionEvent?e)?{

String?text?=?urlInput.getText();

if?(text?==?null?||?text.length()?==?0)?{

viewArea.setText("您沒有輸入URL");

return;

}

try?{

URL?url?=?new?URL(text);

String?context?=?getContent(url);

if?(context?!=?null)?{

searchFromText(context);

}

}?catch?(MalformedURLException?e1)?{

viewArea.setText("您輸入的URL不合法:"?+?text);

}

}

});

}

private?String?getContent(URL?url)?{

StringBuffer?builder?=?new?StringBuffer();

int?responseCode?=?-1;

HttpURLConnection?con?=?null;

try?{

con?=?(HttpURLConnection)?url.openConnection();

con.setRequestProperty("User-Agent",

"Mozilla/4.0?(compatible;?MSIE?5.0;?Windows?NT;?DigExt)");//?IE代理進行下載

con.setConnectTimeout(60000);

con.setReadTimeout(60000);

//?獲得網頁返回信息碼

responseCode?=?con.getResponseCode();

if?(responseCode?==?-1)?{

viewArea.setText("連接失敗:"?+?url.toString());

return?null;

}

if?(responseCode?=?400)?{

viewArea.setText("請求失敗,錯誤碼:"?+?responseCode);

return?null;

}

InputStream?is?=?con.getInputStream();

InputStreamReader?isr?=?new?InputStreamReader(is);

BufferedReader?br?=?new?BufferedReader(isr);

String?str?=?null;

while?((str?=?br.readLine())?!=?null)

builder.append(str);

is.close();

}?catch?(IOException?e)?{

e.printStackTrace();

viewArea.setText("IOException:?"?+?url.toString());

}?finally?{

con.disconnect();

}

return?builder.toString();

}

private?void?searchFromText(String?context)?{

viewArea.setText("查找URL中:\n");

Pattern?pattern?=?Pattern.compile("a(?[^]+)*(.*?)/a");

Matcher?matcher?=?pattern.matcher(context);

while?(matcher.find())?{

for?(String?prop?:?matcher.group(1).split("?"))?{

int?indexOf?=?prop.indexOf('=');

if?(indexOf??0)?{

if?(prop.substring(0,?indexOf).equals("href"))?{

String?url2?=?prop.substring(indexOf?+?2,?prop.length()?-?1);

viewArea.append(url2?+?"\n");

}

}

}

}

}

}

文章名稱:java下載url代碼 java下載路徑
URL標題:http://www.muchs.cn/article20/dosshjo.html

成都網站建設公司_創(chuàng)新互聯,為您提供網站收錄、網站維護、網站改版、網站設計公司、ChatGPT搜索引擎優(yōu)化

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

外貿網站建設