【发布时间】:2014-06-19 23:50:26
【问题描述】:
我有一个简单的代码,我用它的 url 来下载图像,我的代码运行良好,但由于某种原因,我不明白为什么我不能下载以下图像:http://www.plazavea.com.pe/RepositorioAPS/0/0/cat/37/FOLLETO23_CLIENTE10.jpg 如果我尝试下载另一个图像,它会运行良好(例如:http://www.soyunalbondiga.com/wp-content/uploads/2013/05/obiwan.jpg)。 此外,我看到当我用那个 url(坏的那个)运行我的程序时,它打印出 ContentType 是 html/text,但是当我把这个 url 放在浏览器中时,它显示的图像没有问题。 我需要从该域 (www.plazavea.com.pe) 下载图像。请您帮忙。
public static void main(String[] args) {
try {
// Url con la foto
URL url = new URL(
"http://plazavea.com.pe/RepositorioAPS/0/0/cat/37/FOLLETO23_CLIENTE10.jpg");
// establecemos conexion
URLConnection urlCon = url.openConnection();
// Sacamos por pantalla el tipo de fichero
System.out.println(urlCon.getContentType());
// Se obtiene el inputStream de la foto web y se abre el fichero
// local.
InputStream is = urlCon.getInputStream();
FileOutputStream fos = new FileOutputStream("d:/foto.jpg");
// Lectura de la foto de la web y escritura en fichero local
byte[] array = new byte[1000]; // buffer temporal de lectura.
int leido = is.read(array);
while (leido > 0) {
fos.write(array, 0, leido);
leido = is.read(array);
}
// cierre de conexion y fichero.
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
感谢您的帮助。
卡洛斯。
【问题讨论】:
-
你在文件中写了什么?
-
嗨,Brett,我只想下载这张图片:plazavea.com.pe/RepositorioAPS/0/0/cat/37/… 使用 java 程序。谢谢。
-
那么您下载的内容是 html/text 并写入文件,该文件的内容是什么?
标签: java