[英]Getting Image from URL (Java)
我正在嘗試閱讀以下圖片
但它顯示 IIOException。
這是代碼:
Image image = null;
URL url = new URL("http://bks6.books.google.ca/books?id=5VTBuvfZDyoC&printsec=frontcover&img=1& zoom=5&edge=curl&source=gbs_api");
image = ImageIO.read(url);
jXImageView1.setImage(image);
這段代碼對我來說很好用。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class SaveImageFromUrl {
public static void main(String[] args) throws Exception {
String imageUrl = "http://www.avajava.com/images/avajavalogo.jpg";
String destinationFile = "image.jpg";
saveImage(imageUrl, destinationFile);
}
public static void saveImage(String imageUrl, String destinationFile) throws IOException {
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
}
您收到HTTP 400
(錯誤請求)錯誤,因為您的 URL 中有space
。 如果您修復它(在zoom
參數之前),您將收到HTTP 400
錯誤(未經授權)。 也許您需要一些 HTTP 標頭來將您的下載標識為可識別的瀏覽器(使用“User-Agent”標頭)或其他身份驗證參數。
對於用戶代理示例,然后使用ImageIO.read(InputStream)使用連接輸入流:
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "xxxxxx");
使用xxxxxx
所需的任何東西
試試這個:
//urlPath = address of your picture on internet
URL url = new URL("urlPath");
BufferedImage c = ImageIO.read(url);
ImageIcon image = new ImageIcon(c);
jXImageView1.setImage(image);
直接調用 URL 獲取圖像可能會涉及重大安全問題。 您需要確保您有足夠的權限來訪問該資源。 但是您可以使用ByteOutputStream
來讀取圖像文件。 這是一個示例(這只是一個示例,您需要根據您的要求進行必要的更改。)
ByteArrayOutputStream bis = new ByteArrayOutputStream();
InputStream is = null;
try {
is = url.openStream ();
byte[] bytebuff = new byte[4096];
int n;
while ( (n = is.read(bytebuff)) > 0 ) {
bis.write(bytebuff, 0, n);
}
}
嘗試:
public class ImageComponent extends JComponent {
private final BufferedImage img;
public ImageComponent(URL url) throws IOException {
img = ImageIO.read(url);
setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
}
public static void main(String[] args) throws Exception {
final URL kitten = new URL("https://placekitten.com/g/200/300");
final ImageComponent image = new ImageComponent(kitten);
JFrame frame = new JFrame("Test");
frame.add(new JScrollPane(image));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
您可以嘗試使用此類來顯示從 JFrame 中的 URL 讀取的圖像。
public class ShowImageFromURL {
public static void show(String urlLocation) {
Image image = null;
try {
URL url = new URL(urlLocation);
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
conn.connect();
InputStream urlStream = conn.getInputStream();
image = ImageIO.read(urlStream);
JFrame frame = new JFrame();
JLabel lblimage = new JLabel(new ImageIcon(image));
frame.getContentPane().add(lblimage, BorderLayout.CENTER);
frame.setSize(image.getWidth(null) + 50, image.getHeight(null) + 50);
frame.setVisible(true);
} catch (IOException e) {
System.out.println("Something went wrong, sorry:" + e.toString());
e.printStackTrace();
}
}
}
參考: https : //gist.github.com/aslamanver/92af3ac67406cfd116b7e4e177156926
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.