Java 如何在不使用文件的情况下执行此操作?

Java 如何在不使用文件的情况下执行此操作?,java,Java,我有以下代码: Connection.Response captchaResponse = Jsoup.connect(CAPTCHA_URL) .timeout(3000) .cookies(cookies) .userAgent("Mozilla/5.0") .method(Method.GET) .ignoreContentType

我有以下代码:

Connection.Response captchaResponse = Jsoup.connect(CAPTCHA_URL)
                .timeout(3000)
                .cookies(cookies)
                .userAgent("Mozilla/5.0")
                .method(Method.GET)
                .ignoreContentType(true)
                .execute();

        cookies.putAll(captchaResponse.cookies());


        // writing captcha image to file
        FileOutputStream fileWriter = new FileOutputStream(new File(CAPTCHA_FILENAME));
        fileWriter.write(captchaResponse.bodyAsBytes());
        fileWriter.close();

        showImage(CAPTCHA_FILENAME,"captcha");
showImage
功能:

public void showImage(String filename,String title) throws IOException
{
    ImageIcon icon = new ImageIcon(ImageIO.read(new File(filename)));
    JFrame frame = new JFrame(title);
    JLabel imageLabel = new JLabel();
    imageLabel.setIcon(icon);
    frame.add(imageLabel);
    frame.setSize(100,100);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}

我有一个需要显示的验证码。为此,我将验证码存储在一个文件中,并每次读取该文件。有没有什么方法可以在不涉及文件的情况下执行此操作?

您可以使用
ByteArrayOutputStream
而不是
FileOutputStream
来存储验证码。然后:

ByteArrayInputStream imageInput=new ByteArrayInputStream(outputStream.toByteArray());
要将
ByteArrayOutputStream
转换为
ByteArrayInputStream
以在
showImage
中使用此代码,请执行以下操作:

ImageIO.read(imageInput);

这是什么
showImage()
?在窗口(JFrame)中显示验证码的函数。我不太明白这个问题。例如,您可以将验证码存储在数据库中。或者在记忆中。有点不清楚为什么要避免使用文件。@FlorianSchaetz,我只需要验证码一次,为什么要将其存储在文件中?@cyberPheonix很乐意帮助!