url下載java
Ⅰ 安卓如何實現輸入url通過url將網路資源下載並儲存到本地(無論什麼文件類型都可以下載)
主要代碼
package com.android.xiong.urltest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView show;
Bitmap bitmap;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
// 使用ImageView顯示該圖片
show.setImageBitmap(bitmap);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (ImageView) findViewById(R.id.show);
new Thread() {
@Override
public void run() {
// 定義一個URL對象
URL url;
try {
url = new URL(
圖片);
// 打開該URL的資源輸入流
InputStream is = url.openStream();
// 從InputStream中解析出圖片
bitmap = BitmapFactory.decodeStream(is);
// 發送消息
handler.sendEmptyMessage(0x123);
is.close();
// 再次打開RL對應的資源輸入流
is = url.openStream();
// 打開手機文件對應的輸出流
OutputStream os = openFileOutput("KEQIANG.JPG", MODE_APPEND);
byte[] buff = new byte[1024];
int hasRead = 0;
// 將URL資源下載到本地
while ((hasRead = is.read(buff)) > 0) {
os.write(buff, 0, hasRead);
}
is.close();
os.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
[html] view plain
<LinearLayout xmlns:android=網址
xmlns:tools=網址
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/hello_world"/>
</LinearLayout>
[html] view plain
</pre><pre code_snippet_id="86820" snippet_file_name="blog_20131128_4_1113442" name="code" class="html"> <uses-permission
android:name="android.permission.INTERNET"/>
Ⅱ 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("file:///" + 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的url下載器怎麼在按鈕的監聽器里實現暫停和繼續操作,求代碼
實際上就是斷點續傳。思路是在開始按鈕點擊的時候訪問(/新建)文件,移動文件指針到上次記錄的位置,下載的時候在HttpURLConnection類下有一個setRequestProperties("range","byte=xxx")。
點擊暫停的時候記錄已經下載的位元組數,就是上面的xxx變數。
具體看java IO流,RandAccessFile類,HTTP協議
Ⅳ 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在哪下載比較好
可以到sun的官方網站http://www.java.com/zh_CN/下載jre,只用於運行Java程序
如果你是開發人員,可以到http://www.oracle.com/technetwork/java/javase/downloads/index.html下載最新版的jdk,網頁是英文的,可以用Google翻譯或者bing翻譯
Ⅵ 我用java做了一個通過url地址下載指定文件的功能,文件名可能包含中文,IE正常,火狐失敗.
您好!很高興為您答疑!
火狐下您可以安裝Firebug檢查頁面代碼,它集HTML查看和編輯、Javascript控制台、網路狀況監視器於一體,是開發JavaScript、CSS、HTML和Ajax的得力助手。
您可以在火狐社區了解更多內容。希望我的回答對您有所幫助,如有疑問,歡迎繼續在本平台咨詢。
Ⅶ java中URL下載文件的問題。程序有問題,下載的mp3文件比原來的大,請問問題出在哪裡
is.read()的返回實際從網上讀取的位元組數,而你是只按2KB寫入,導致多寫
整個while改成
intc;
while((c=is.read(bfr))!=-1){
fos.write(bfr,0,c);
}