下载网页图片用什么方法好? 用BufferedReader直接读取url,转换成byte字节写入记事本,更改后缀名为jpg格式,这样可行吗?不然怎样做比较简单快捷? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的方法应该可行,你可以写个小程序试一下,在你写入文件中时,文件名字就用XX.jpg就OK了, 应该可以吧。也可以用base64。 对非文本文件不建议使用BufferedReader读取,可以使用BufferedInputStream例如:import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;public class GetImage { public static void main(String[] args) { try { String url = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.gif"; BufferedInputStream in = new BufferedInputStream(new URL(url).openStream()); File img = new File("google.gif"); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(img)); byte[] buf = new byte[2048]; int length = in.read(buf); while(length != -1) { out.write(buf,0,length); length = in.read(buf); } in.close(); out.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} 用BufferedReader直接读取url,转换成byte字节写入记事本方法是可以但是你这种方法存在很大的问题当文件、图片过大时、速度很慢 要速度吗?使用多线程吧,以下是demo:package web;import java.io.InputStream;import java.io.RandomAccessFile;public class DownThread extends Thread{ // 定义字节数组(取水的竹筒)的长度 private final int BUFF_LEN = 32;// 定义下载的起始点 private long start;// 定义下载的结束点 private long end;// 下载资源对应的输入流 private InputStream is;// 将下载到的字节输出到raf中 private RandomAccessFile raf;// 构造器,传入输入流,输出流和下载起始点、结束点 public DownThread(long start, long end, InputStream is, RandomAccessFile raf) { // 输出该线程负责下载的字节位置 System.out.println(start + "---->" + end); this.start = start; this.end = end; this.is = is; this.raf = raf; } public void run() { try { is.skip(start); raf.seek(start); // 定义读取输入流内容的缓存数组(竹筒) byte[] buff = new byte[BUFF_LEN];// 本线程负责下载资源的大小 long contentLen = end - start;// 定义最多需要读取几次就可以完成本线程的下载 long times = contentLen / BUFF_LEN + 4;// 实际读取的字节数 int hasRead = 0; for (int i = 0; i < times; i++) { hasRead = is.read(buff);// 如果读取的字节数小于0,则退出循环! if (hasRead < 0) { break; } raf.write(buff, 0, hasRead); } } catch (Exception ex) { ex.printStackTrace(); }// 使用finally块来关闭当前线程的输入流、输出流 finally { try { if (is != null) { is.close(); } if (raf != null) { raf.close(); } } catch (Exception ex) { ex.printStackTrace(); } } }}package web;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.URL;import java.net.URLConnection;public class MutilDown{ public static void main(String[] args) { final int DOWN_THREAD_NUM = 4; final String OUT_FILE_NAME = "d://down.jpg"; InputStream[] isArr = new InputStream[DOWN_THREAD_NUM]; RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM]; try { // 创建一个URL对象 URL url = new URL( "http://images.china-pub.com/ebook35001-40000/35850/shupi.jpg"); // 以此URL对象打开第一个输入流 isArr[0] = url.openStream(); long fileLen = getFileLength(url); System.out.println("网络资源的大小" + fileLen); // 以输出文件名创建第一个RandomAccessFile输出流 outArr[0] = new RandomAccessFile(OUT_FILE_NAME, "rw"); // 创建一个与下载资源相同大小的空文件 for (int i = 0; i < fileLen; i++) { outArr[0].write(0); } // 每线程应该下载的字节数 long numPerThred = fileLen / DOWN_THREAD_NUM;// 整个下载资源整除后剩下的余数 long left = fileLen % DOWN_THREAD_NUM; for (int i = 0; i < DOWN_THREAD_NUM; i++) { // 为每个线程打开一个输入流、一个RandomAccessFile对象,//让每个线程分别负责下载资源的不同部分。 if (i != 0) { // 以URL打开多个输入流 isArr[i] = url.openStream(); // 以指定输出文件创建多个RandomAccessFile对象 outArr[i] = new RandomAccessFile(OUT_FILE_NAME, "rw"); } // 分别启动多个线程来下载网络资源 if (i == DOWN_THREAD_NUM - 1) { // 最后一个线程下载指定numPerThred+left个字节 new DownThread(i * numPerThred, (i + 1) * numPerThred + left, isArr[i], outArr[i]).start(); } else { // 每个线程负责下载一定的numPerThred个字节 new DownThread(i * numPerThred, (i + 1) * numPerThred, isArr[i], outArr[i]).start(); } } } catch (Exception ex) { ex.printStackTrace(); } } // 定义获取指定网络资源的长度的方法 public static long getFileLength(URL url) throws Exception { long length = 0;// 打开该URL对应的URLConnection。 URLConnection con = url.openConnection(); // 获取连接URL资源的长度 long size = con.getContentLength(); length = size; return length; }} 初学者问题求教,请各位大神指教 java 套接字编程的,实在搞不定了。 JAVA 小问题 java数据结构算法里的变位字程序有一个地方搞不懂,请高人指教 请教大侠们!java简单问题? 请大家帮个忙! 请帮我指点方向,谢谢 关于socket的问题,高手请进!!! 这里能问关于swing的问题不? 怎样实现照片上传 有关Date问题 关于运行时异常的处理机制
也可以用base64。
例如:import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;public class GetImage { public static void main(String[] args) {
try {
String url = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.gif";
BufferedInputStream in = new BufferedInputStream(new URL(url).openStream());
File img = new File("google.gif");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(img));
byte[] buf = new byte[2048];
int length = in.read(buf);
while(length != -1) {
out.write(buf,0,length);
length = in.read(buf);
}
in.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.RandomAccessFile;public class DownThread extends Thread
{
// 定义字节数组(取水的竹筒)的长度
private final int BUFF_LEN = 32;// 定义下载的起始点
private long start;// 定义下载的结束点
private long end;// 下载资源对应的输入流
private InputStream is;// 将下载到的字节输出到raf中
private RandomAccessFile raf;// 构造器,传入输入流,输出流和下载起始点、结束点 public DownThread(long start, long end, InputStream is, RandomAccessFile raf)
{
// 输出该线程负责下载的字节位置
System.out.println(start + "---->" + end);
this.start = start;
this.end = end;
this.is = is;
this.raf = raf;
} public void run()
{
try
{
is.skip(start);
raf.seek(start); // 定义读取输入流内容的缓存数组(竹筒)
byte[] buff = new byte[BUFF_LEN];// 本线程负责下载资源的大小
long contentLen = end - start;// 定义最多需要读取几次就可以完成本线程的下载
long times = contentLen / BUFF_LEN + 4;// 实际读取的字节数
int hasRead = 0;
for (int i = 0; i < times; i++)
{
hasRead = is.read(buff);// 如果读取的字节数小于0,则退出循环!
if (hasRead < 0)
{
break;
}
raf.write(buff, 0, hasRead);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}// 使用finally块来关闭当前线程的输入流、输出流
finally
{
try
{
if (is != null)
{
is.close();
}
if (raf != null)
{
raf.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}package web;import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;public class MutilDown
{
public static void main(String[] args)
{
final int DOWN_THREAD_NUM = 4;
final String OUT_FILE_NAME = "d://down.jpg";
InputStream[] isArr = new InputStream[DOWN_THREAD_NUM];
RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM];
try
{
// 创建一个URL对象
URL url = new URL(
"http://images.china-pub.com/ebook35001-40000/35850/shupi.jpg");
// 以此URL对象打开第一个输入流
isArr[0] = url.openStream();
long fileLen = getFileLength(url);
System.out.println("网络资源的大小" + fileLen);
// 以输出文件名创建第一个RandomAccessFile输出流
outArr[0] = new RandomAccessFile(OUT_FILE_NAME, "rw");
// 创建一个与下载资源相同大小的空文件
for (int i = 0; i < fileLen; i++)
{
outArr[0].write(0);
}
// 每线程应该下载的字节数
long numPerThred = fileLen / DOWN_THREAD_NUM;// 整个下载资源整除后剩下的余数
long left = fileLen % DOWN_THREAD_NUM;
for (int i = 0; i < DOWN_THREAD_NUM; i++)
{
// 为每个线程打开一个输入流、一个RandomAccessFile对象,//让每个线程分别负责下载资源的不同部分。
if (i != 0)
{
// 以URL打开多个输入流
isArr[i] = url.openStream();
// 以指定输出文件创建多个RandomAccessFile对象
outArr[i] = new RandomAccessFile(OUT_FILE_NAME, "rw");
}
// 分别启动多个线程来下载网络资源
if (i == DOWN_THREAD_NUM - 1)
{
// 最后一个线程下载指定numPerThred+left个字节
new DownThread(i * numPerThred, (i + 1) * numPerThred
+ left, isArr[i], outArr[i]).start();
}
else
{
// 每个线程负责下载一定的numPerThred个字节
new DownThread(i * numPerThred, (i + 1) * numPerThred,
isArr[i], outArr[i]).start();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
} // 定义获取指定网络资源的长度的方法
public static long getFileLength(URL url) throws Exception
{
long length = 0;// 打开该URL对应的URLConnection。
URLConnection con = url.openConnection();
// 获取连接URL资源的长度
long size = con.getContentLength();
length = size;
return length;
}}