gpt4 book ai didi

java - 使用java代码从互联网下载图片的问题

转载 作者:行者123 更新时间:2023-12-02 07:47:32 26 4
gpt4 key购买 nike

以下代码不起作用:

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DownloadFile extends Thread {

URL url;
long startPos;
long endPos;
int length;
public static ExecutorService pool = Executors.newCachedThreadPool();

public DownloadFile(URL url, long startPos, long endPos, int length) {
this.url = url;
this.startPos = startPos;
this.endPos = endPos;
this.length = length;
}

public static void main(String[] args) {
try {
download(new URL(
"http://hiphotos.baidu.com/dazhu1115/pic/item/fb8eccffa223a373d6887dfc.jpg"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void download(URL url) throws IOException {
String fileName = url.getFile();
final long size = 8096;
if (fileName.equals("")) {
throw new IOException("download webfile \"" + fileName
+ "\" failed");
}
HttpURLConnection con = (HttpURLConnection) url.openConnection();
long total = con.getContentLength();
System.out.println("file size is " + total);
con.disconnect();
if (total <= size) {
// con = (HttpURLConnection) url.openConnection();
// con.connect();
// in = con.getInputStream();
// return download(fileName, in);
// con.disconnect();
} else {
long part = total % size != 0 ? total / size + 1 : total / size;
long len;
byte[] b = null;
for (int i = 0; i < part; i++) {
len = i != part - 1 ? size : total % size;
DownloadFile dl = new DownloadFile(url, size * i, size
* (i + 1), (int) len);
pool.execute(dl);
}
}
}

public void run() {
byte[] b = new byte[length];
HttpURLConnection con;
try {
con = (HttpURLConnection) url.openConnection();
con.setAllowUserInteraction(true);
con.setReadTimeout(30000);
con.setRequestProperty("RANGE", "bytes=" + startPos + "-" + endPos);
// con.setRequestProperty("GET", fileName + " HTTP/1.1");
// con.setRequestProperty("Accept","image/gif,image/x-xbitmap,application/msword");
// con.setRequestProperty("Connection", "Keep-Alive");
con.connect();
/*
* in = con.getInputStream(); DataInputStream dis = new
* DataInputStream(in);
*/
BufferedInputStream bis = new BufferedInputStream(
con.getInputStream());
int wlen = bis.read(b, 0, length);
RandomAccessFile oSavedFile = new RandomAccessFile("D:/bbb.jpg",
"rw");
oSavedFile.seek(startPos);
oSavedFile.write(b, 0, wlen);
// con.disconnect();
bis.close();
oSavedFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

下载的图片大小正确,但无法正常查看该图片的图像。我不知道技巧在哪里。下面是我下载的图片 enter image description here

问题已解决,将我的代码更改为:

package com.tom.labs;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DownloadFile extends Thread {

URL url;
long startPos;
long endPos;
final static int bufferSize = 1024 * 3;
public static ExecutorService pool = Executors.newCachedThreadPool();

public DownloadFile(URL url, long startPos, long endPos) {
this.url = url;
this.startPos = startPos;
this.endPos = endPos;
}

public static void main(String[] args) {
try {
download(new URL("http://hiphotos.baidu.com/dazhu1115/pic/item/fb8eccffa223a373d6887dfc.jpg"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void download(URL url) throws IOException {
String fileName = url.getFile();
final long size = 8096;
if (fileName.equals("")) {
throw new IOException("download webfile \"" + fileName + "\" failed");
}
HttpURLConnection con = (HttpURLConnection) url.openConnection();
long total = con.getContentLength();
System.out.println("file size is " + total);
con.disconnect();
if (total <= size) {

} else {
long part = total % size != 0 ? total / size + 1 : total / size;
long len;
byte[] b = null;
for (int i = 0; i < part; i++) {
len = i != part - 1 ? size : total % size;
long startPos = size * i;
long endPos = startPos + len - 1;
con = (HttpURLConnection) url.openConnection();
con.setAllowUserInteraction(true);
con.setReadTimeout(30000);
con.setRequestProperty("RANGE", "bytes=" + startPos + "-" + endPos);
System.out.println("Request Data from " + startPos + " to " + endPos);
con.connect();

DownloadFile task = new DownloadFile(url,startPos,endPos);
pool.execute(task);
}
}
}

public void run() {
byte[] b = new byte[bufferSize];
HttpURLConnection con;
try {
con = (HttpURLConnection) url.openConnection();
con.setAllowUserInteraction(true);
con.setReadTimeout(30000);
con.setRequestProperty("RANGE", "bytes=" + startPos + "-" + endPos);
con.connect();
BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
RandomAccessFile oSavedFile = new RandomAccessFile("D:/bbb.jpg", "rw");
oSavedFile.seek(startPos);
System.out.println("Request Data from " + startPos + " to " + endPos);
while (startPos < endPos) {
int wlen = bis.read(b, 0, bufferSize);
oSavedFile.write(b, 0, wlen);
startPos += wlen;
}
bis.close();
oSavedFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

最佳答案

如果没有更多可用字节,则 BufferedIntpuStream.read 方法返回所以你必须循环直到你写完整个 block 的大小。

这里是解决您的问题的方法:

       BufferedInputStream bis = new BufferedInputStream(
con.getInputStream());


RandomAccessFile oSavedFile = new RandomAccessFile("D:/bbb.jpg",
"rw");
oSavedFile.seek(startPos);
int wlen = 0;
while (wlen < length) {
int read = bis.read(b, 0, length);
wlen += read;
oSavedFile.write(b, 0, read);
}

关于java - 使用java代码从互联网下载图片的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10629387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com