ZipArchive文件中的Json不支持ISO-8859-1格式

时间:2013-09-10 07:09:11

标签: json servlets zip

我正在通过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格式数据显示为????

1 个答案:

答案 0 :(得分: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);