0

我有一个在 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 中也可以工作。自从我更新到最新版本后,它就出现了这个问题。

在再次使用小程序之前,我是否需要确保关闭某些东西?在过去的两天里,我一直在反对这个。当然希望有人可以帮助...

4

1 回答 1

0

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

于 2012-12-26T17:10:50.037 回答