【问题标题】:Image Download in Java [duplicate]Java中的图像下载[重复]
【发布时间】:2014-12-30 05:10:22
【问题描述】:

此代码应该下载网页中指定的图像,但它会抛出

线程“main”javax.net.ssl.SSLProtocolException 中的异常:握手警报:无法识别的名称

请帮我解决这个问题。我使用 NetBeans 7.1.1 进行了测试。

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class Download {

    public static void main(final String[] args) throws Exception {
        String fileName = "Google_logo.png";
        String website = "https://img3.wikia.nocookie.net/cb20100520131746/logopedia/images/5/5c/" + fileName;
        System.out.println("Downloading File From: " + website);
        URL url = new URL(website);
        InputStream inputStream = url.openStream();
        OutputStream outputStream = new FileOutputStream(fileName);
        byte[] buffer = new byte[2048];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            System.out.println("Buuffer Read of length :" + length);
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }
}

【问题讨论】:

    标签: java netbeans


    【解决方案1】:

    通常当你想用浏览器下载某个网站的图片或文件时,你会向服务器发送一个 Http 请求,服务器会返回一个响应。浏览器从响应中读取内容,并通过弹出“另存为”窗口询问您将下载内容存储在何处。

    对于您的程序,您只需打开某个 URL 的连接并尝试将此连接信息写入某处。您需要做的是伪造对某个 URL 的 http 请求,捕获响应并提取响应内容以输出到您想要的位置。 HttpClient 可以帮助您做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2016-05-24
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      相关资源
      最近更新 更多