import javax.swing.*;
import java.net.MalformedURLException;
import java.net.URL;public class Aloha extends JPanel {
private static void createAndShowGui() throws MalformedURLException {
ImageIcon icon = new ImageIcon(new URL("http://www.cppblog.com/images/cppblog_com/biao/Button.png"));
JLabel label = new JLabel(icon); JFrame frame = new JFrame();
frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGui();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
}
怎么看上去就那么别扭呢!
一,怎么解析显示图片,网页地址不是图片格式的,如ASP ASPX JSP等等。
二,怎么拉伸显示。你那个代码只是直接把图片扔在上面的。
三,因为是验证码图片。所以会有COOKIE的。请不要直接用URL来显示图片,要先下载BYTE。再显示。这样下载的时候我就可以自己存一份COOKIE了。再次谢谢了!
我怀疑有可能本来这些字节流就多出来了其它信息。或者解析的时候不正常。只得到字节流。
我也不知道这张图片具体格式是什么。这些原因也许有。
这网页的源码我提取出来了。
<img src="http://www.uxin.com/captcha/index.html">
怎么都是这个地址,无法显示到组件上。但网页上正常显示,不知为何?
执行下面的代码看看得到了什么。
HttpURLConnection connection = (HttpURLConnection) new URL("http://www.uxin.com/captcha/index.html?t=" + new Date().getTime()).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream response = connection.getInputStream();
byte[] buf = new byte[1024]; FileOutputStream out = new FileOutputStream("/Users/Biao/Desktop/x.html"); int len = 0;
while ((len = response.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
out.close();
你也可以看到的,为何在网页上打开就是图片了。我直接下载却成了网页,???
因为需要加HEAD才能正常得到见面数据。JAVA就是这么麻烦。
connection.setRequestProperty("Accept","text/html, application/xhtml+xml, */*");
这东西全部加上即可。结帖给分,再次谢谢。有时候讨论也是一种帮助。