當實例化一個新的 URL 時,下面的第一段代碼來自 DrawImage 類。但是,當我從另一個類 RandomImage 輸入相同的字符串(“ https://www.purchased.com/hubfs/template/favicon.png ”)時,會出現以下錯誤:我首先嘗試手動輸入字符串。import java.awt.Image;import java.io.IOException;import java.net.URL;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;public class DrawImage { public static void main(String[] args) throws Exception { String link = "https://www.purchased.com/hubfs/template/favicon.png"; System.setProperty("http.agent", "Chrome"); URL url = new URL(link); Image image = ImageIO.read(url.openStream()); }}-> 按預期工作然后,我改為提供一個 RandomImage(這是產生錯誤消息的那個!)import java.awt.Image;import java.io.IOException;import java.net.URL;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;public class DrawImage { public static void main(String[] args) throws Exception { RandomImage img = new RandomImage(); String link = img.link; System.setProperty("http.agent", "Chrome"); URL url = new URL(link); Image image = ImageIO.read(url.openStream()); }}以下課程提供隨機圖像:import java.net.*;import java.io.*public class RandomImage { public static String link; public RandomImage() throws Exception { this.link = generateUsableImageLink(); }但是在運行第二種方法時,出現以下錯誤:java.net.MalformedURLException: no protocol: "https://www.purchased.com/hubfs/template/favicon.png" at java.net.URL.<init>(Unknown Source) at java.net.URL.<init>(Unknown Source) at java.net.URL.<init>(Unknown Source) at DrawImage.main(DrawImage.java:25)我嘗試(如您在上面的代碼中看到的那樣)將 RandomImage 設為一個對象,但錯誤提要告訴我,當我使用來自 RandomImage 的輸入創建 URL 實例時發生了錯誤。如果我猜的話,這個問題是由導入包之間的某些交互或 RandomImage 中拋出的錯誤引起的。一個字符一個字符,我手動輸入的String和RandomImage生成的String完全一致。我將如何解決這個問題?
添加回答
舉報
0/150
提交
取消