2

我正在通过 servlet 中的 zip 存档文件下载 json 文件。但是当下载我的数据时,ISO-8859-1 格式已损坏。代码给出了 velow

private void doProcess(HttpServletRequest req, HttpServletResponse resp) {

        // -----------Compression---------
        String feed = "Topics";
        resp.setContentType("application/zip;");
        resp.addHeader("Content-Disposition", "attachment; filename=\"" + feed
                + ".zip\"");

        OutputStream out = null;
        try {
            out = resp.getOutputStream();

        } catch (IOException e1) {
            e1.printStackTrace();
        }

        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
        zipOutputStream.setLevel(9);

        ArrayList<Topic> topics = new ArrayList<Topic>();
        topics = new TopicsDBHelper().getTopics(con);

        try {

            // -----------Add to Zip----------------------------

            ZipEntry entry = new ZipEntry(feed + ".json");

            Gson gs = new GsonBuilder().disableHtmlEscaping().create();

            byte[] content = gs.toJson(topics).getBytes();
            InputStream is = null;
            BufferedReader bfReader = null;
            is = new ByteArrayInputStream(content);
            bfReader = new BufferedReader(new InputStreamReader(is));

            BufferedReader reader = new BufferedReader(bfReader);

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                    zipOutputStream, Charset.forName("ISO-8859-1")));

            zipOutputStream.putNextEntry(entry);

            // this is the important part:
            // all character data is written via the writer and not the zip
            // output stream
            String line = null;
            while ((line = reader.readLine()) != null) {
                writer.append(line).append('\n');
            }
            writer.flush(); // i've used a buffered writer, so make sure to
                            // flush to the
            // underlying zip output stream

            zipOutputStream.closeEntry();
            zipOutputStream.finish();

            reader.close();
            writer.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

以下数据来自 JSON 文件:

[{"description":"k?????? ?????U ????B?{ ???t?? ????cBZA? ???G?? ????AW ???{???? ?Z????{????x? ??? ?? ??? ?????X ?????? ?W??K?.","dId":2,"dname":"jahan","name":"World","status":2,"timestamp":0,"tId":2}]

JSON 文件通过 zip 文件下载。我的 ISO-8859-1 格式数据显示为 ????

4

1 回答 1

0
 i have referred this question (http://stackoverflow.com/questions/11276343/zipinputstreaminputstream-charset-decodes-zipentry-file-name-falsely) and corrected my answer. Then its working


ZipEntry entry = new ZipEntry(feed + ".json");

            Gson gs = new GsonBuilder().disableHtmlEscaping().create();

            byte[] content = gs.toJson(topics).getBytes("Cp437");
            InputStream is = null;
            BufferedReader bfReader = null;
            is = new ByteArrayInputStream(content);
            bfReader = new BufferedReader(new InputStreamReader(is, "Cp437"));

            BufferedReader reader = new BufferedReader(bfReader);

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                    zipOutputStream, Charset.forName("UTF-8")));

            zipOutputStream.putNextEntry(entry);
于 2013-09-10T09:45:30.870 回答