当前位置:首页 » 编程语言 » url下载java

url下载java

发布时间: 2023-08-30 01:41:21

Ⅰ 安卓如何实现输入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);
}
热点内容
编程课v 发布:2025-02-04 08:45:00 浏览:103
模拟器能有手机脚本么 发布:2025-02-04 08:39:50 浏览:755
android显示html图片 发布:2025-02-04 08:35:31 浏览:790
如何查学信网账号及密码 发布:2025-02-04 08:33:55 浏览:501
linux32位jdk 发布:2025-02-04 08:33:55 浏览:246
康佳服务器连接失败是怎么回事 发布:2025-02-04 08:18:51 浏览:916
编译编译有什么 发布:2025-02-04 08:05:52 浏览:735
让外网访问内网服务器 发布:2025-02-04 08:02:20 浏览:783
奶块脚本菜地 发布:2025-02-04 07:46:35 浏览:238
条形码识别源码 发布:2025-02-04 07:45:55 浏览:457