获取HttpUrlConnection输出流时,Java Applet在IE中挂起

时间:2012-12-14 21:33:57

标签: java applet httpurlconnection

我有一个已在Chrome中运行的已签名文件上传器小程序,但在IE中运行时似乎挂起了。在IE中,我可以成功上传一个文件(或一组文件),但下次我尝试上传另一个文件时,浏览器冻结,我必须关闭浏览器。当我尝试检索HttpUrlConnection对象的输出流时,似乎正在发生这种情况。这是我的代码:

public void upload(URL url, URL returnUrl, List<FileIconPanel> files) {
    HttpURLConnection conn = null;

    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setChunkedStreamingMode(1024);
        HttpURLConnection.setFollowRedirects(false);
        conn.setRequestProperty("content-type", "application/zip");
        conn.setRequestMethod("POST");

        //This zip output stream will server as our stream to the server and will zip each file while 
        // it sends it to the server.
        ZipOutputStream out = new ZipOutputStream(conn.getOutputStream());
        for (int i = 0; i < files.size(); i++) {
            //For each file we will create a new entry in the ZIP archive and stream the file into that entry.

            File f = (File) files.get(i).getFile();
            ZipEntry entry = new ZipEntry(f.getName());
            out.putNextEntry(entry);
            InputStream in = new FileInputStream(f);
            int read;
            byte[] buf = new byte[1024];

            while ((read = in.read(buf)) > 0) {
                out.write(buf, 0, read);
            }

            out.closeEntry();
        }

        //Once we are done writing out our stream we will finish building the archive and close the stream.
        out.finish();
        out.close();

        // Now that we have set all the connection parameters and prepared all
        //  the data we are ready to connect to the server.
        conn.connect();

        // read & parse the response
        InputStream is = conn.getInputStream();
        StringBuilder response = new StringBuilder();
        byte[] respBuffer = new byte[4096];
        while (is.read(respBuffer) >= 0) {
            response.append(new String(respBuffer).trim());
        }
        is.close();
    } catch (IOException ioE) {
        ioE.printStackTrace();
        logger.info("An unexpected exception has occurred. Contact your system administrator");
    } catch (Exception e) {
        e.printStackTrace();
        logger.info("An unexpected exception has occurred. Contact your system administrator");
    } finally {
        //Once we are done we want to make sure to disconnect from the server.
        if (conn != null) conn.disconnect();
    }
}

我可以在日志文件中看到它冻结了这一行:

ZipOutputStream out = new ZipOutputStream(conn.getOutputStream());

我第二次尝试上传文件。此代码也可以在java 6下的IE中工作。自从我更新到最新版本以来,它已经出现了这个问题。

我有什么东西要打开,我需要确保在再次使用applet之前关闭它吗?在过去的两天里,我一直在反对这一点。当然希望有人能帮忙......

1 个答案:

答案 0 :(得分:0)

我知道发生了什么,但我不确切知道导致问题的原因的详细信息。我至少想出了一个解决方法......我正在使用jquery对话框来显示applet。我正在动态执行此操作,因此每次打开对话框时,它都会再次构建applet。这很奇怪,因为我实际上每次关闭对话框时都会删除包含applet的div元素,所以我认为这样可以确保旧的applet实例也被销毁。 Chrome似乎足够聪明,可以销毁它并创建一个新的(或类似的东西),但IE有问题。