當前位置:首頁 » 編程語言 » java下載url文件

java下載url文件

發布時間: 2023-03-03 10:12:11

java中URL下載文件的問題。程序有問題,下載的mp3文件比原來的大,請問問題出在哪裡

is.read()的返回實際從網上讀取的位元組數,而你是只按2KB寫入,導致多寫

整個while改成

intc;
while((c=is.read(bfr))!=-1){
fos.write(bfr,0,c);
}

Ⅱ java下載伺服器上的文件到客戶端

java編程方法下載伺服器上的文件到本地客服端,代碼如下:

importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.URL;
importjava.net.URLConnection;

publicclassDownLoad{
publicstaticvoiddownloadFile(URLtheURL,StringfilePath)throwsIOException{
FiledirFile=newFile(filePath);
if(!dirFile.exists()){
//文件路徑不存在時,自動創建目錄
dirFile.mkdir();
}
//從伺服器上獲取圖片並保存
URLConnectionconnection=theURL.openConnection();
InputStreamin=connection.getInputStream();
FileOutputStreamos=newFileOutputStream(filePath+"\123.png");
byte[]buffer=newbyte[4*1024];
intread;
while((read=in.read(buffer))>0){
os.write(buffer,0,read);
}
os.close();
in.close();
}
publicstaticvoidmain(String[]args){
//下面添加伺服器的IP地址和埠,以及要下載的文件路徑
StringurlPath="http://伺服器IP地址:埠/image/123.png";

//下面代碼是下載到本地的位置
StringfilePath="d:\excel";

URLurl=newURL(urlPath);

try{

downloadFile(url,filePath);

}catch(IOExceptione){

e.printStackTrace();

}

}

}

Ⅲ 使用java編寫一個多線程下載器,需要在URL欄中輸入網址,然後通過網址下載。該怎麼實現,求源代碼

swing做前台界面。後台使用java.net中的HTTPConnection下載就OK。下載可以用getInputStream()獲取數據,然後寫入文件。只提供思路,無代碼。不搞java好多年……

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

importjava.awt.BorderLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;

importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;

{
privateJTextFielrlInput;
privateJTextAreaviewArea;

publicstaticvoidmain(String[]args){
newHttpViewer();
}

publicHttpViewer(){
this.setTitle("HttpViewer");
this.setSize(800,600);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initPanel();
initAction();
this.setVisible(true);
}

//這個方法用來設置窗口布局
privatevoidinitPanel(){
JPanelnorthPanel=newJPanel();
JLabelurlInputLabel=newJLabel("URL:");
urlInput=newJTextField(60);
northPanel.add(urlInputLabel);
northPanel.add(urlInput);
this.add(northPanel,BorderLayout.NORTH);

JPanelcenterPanel=newJPanel();
viewArea=newJTextArea(27,60);
centerPanel.add(newJScrollPane(viewArea));
this.add(centerPanel);
}

//這個方法用來設置事件
privatevoidinitAction(){
urlInput.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
Stringtext=urlInput.getText();
if(text==null||text.length()==0){
viewArea.setText("您沒有輸入URL");
return;
}
try{
URLurl=newURL(text);
Stringcontext=getContent(url);
if(context!=null){
searchFromText(context);
}
}catch(MalformedURLExceptione1){
viewArea.setText("您輸入的URL不合法:"+text);
}
}
});
}

privateStringgetContent(URLurl){
StringBufferbuilder=newStringBuffer();

intresponseCode=-1;
HttpURLConnectioncon=null;
try{
con=(HttpURLConnection)url.openConnection();
con.setRequestProperty("User-Agent",
"Mozilla/4.0(compatible;MSIE5.0;WindowsNT;DigExt)");//IE代理進行下載
con.setConnectTimeout(60000);
con.setReadTimeout(60000);

//獲得網頁返回信息碼
responseCode=con.getResponseCode();

if(responseCode==-1){
viewArea.setText("連接失敗:"+url.toString());
returnnull;
}

if(responseCode>=400){
viewArea.setText("請求失敗,錯誤碼:"+responseCode);
returnnull;
}

InputStreamis=con.getInputStream();
InputStreamReaderisr=newInputStreamReader(is);
BufferedReaderbr=newBufferedReader(isr);

Stringstr=null;
while((str=br.readLine())!=null)
builder.append(str);
is.close();
}catch(IOExceptione){
e.printStackTrace();
viewArea.setText("IOException:"+url.toString());
}finally{
con.disconnect();
}
returnbuilder.toString();
}

privatevoidsearchFromText(Stringcontext){
viewArea.setText("查找URL中: ");
Patternpattern=Pattern.compile("<a([^>]+)*>(.*?)</a>");
Matchermatcher=pattern.matcher(context);
while(matcher.find()){
for(Stringprop:matcher.group(1).split("")){
intindexOf=prop.indexOf('=');
if(indexOf>0){
if(prop.substring(0,indexOf).equals("href")){
Stringurl2=prop.substring(indexOf+2,prop.length()-1);
viewArea.append(url2+" ");
}
}
}
}
}

}

Ⅳ Java 利用url下載MP3保存到本地

//mp3Url MP3的URL
InputStream in=new URL(mp3Url).openConnection().getInputStream(); //創建連接、輸入流
FileOutputStream f = nre FileOutputStream("c:\mmm.mp3");//創建文件輸出流
byte [] bb=new byte[1024]; //接收緩存
int len;
while( (len=in.read(bb))>0){ //接收
f.write(bb, 0, len); //寫入文件
}
f.close();
in.close();
基本框架,自己調試修改一下

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

您好!很高興為您答疑!

火狐下您可以安裝Firebug檢查頁面代碼,它集HTML查看和編輯、Javascript控制台、網路狀況監視器於一體,是開發JavaScript、CSS、HTML和Ajax的得力助手。
您可以在火狐社區了解更多內容。希望我的回答對您有所幫助,如有疑問,歡迎繼續在本平台咨詢。

熱點內容
java直播網站源碼 發布:2025-07-04 14:46:35 瀏覽:169
安卓應用市場消費記錄怎麼刪除 發布:2025-07-04 14:39:47 瀏覽:30
知道一個伺服器的ip地址 發布:2025-07-04 14:20:33 瀏覽:597
蘋果7鎖屏密碼怎麼改 發布:2025-07-04 14:04:44 瀏覽:710
P三零是什麼配置 發布:2025-07-04 13:58:41 瀏覽:361
哪個安卓機有長方形home鍵 發布:2025-07-04 13:43:58 瀏覽:861
android腳本錄制 發布:2025-07-04 13:17:47 瀏覽:342
嵌入式和安卓哪個硬體成本高 發布:2025-07-04 13:05:56 瀏覽:229
360代理伺服器怎麼設置 發布:2025-07-04 12:49:49 瀏覽:515
iphone在哪清除緩存 發布:2025-07-04 12:49:38 瀏覽:340