【问题标题】:Exception occurred in target VM: Not in GZIP format目标 VM 中发生异常:不是 GZIP 格式
【发布时间】:2015-05-09 10:35:34
【问题描述】:

我在 java 中有一张地图(1.7 版)

public void doTest() {
    try {
         Map<String, String> data = new HashMap<String, String>();
         data.put("Product", "someProduct");
         data.put("CreatedOn", "Fri May 08 02:25:03 IST 2015");
         data.put("Module", "someService");
         data.put("Type", "ERROR");
         data.put("Message", "LogId:635666739033286524 Message:WSClient.FetchReservation::WS FetchReservation calledSystem.Net.WebException: The operation has timed out at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request) at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) at .BOTWSClient.ChannelDirectWS.YieldGain.fFetchReservation(String strReservationXML) at BOTWSClient.WSClient.FetchReservation(String RequestXML) ");
         data.put("IP", "");
         data.put("Name", "WriteLog");

         byte[] byteArray = convert(data);
         int response = port.writelogcollection(byteArray);

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

  public byte[] convert(Map obj) throws IOException {
   ObjectOutputStream os = null;

  ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  os = new ObjectOutputStream(new GZIPOutputStream(byteStream));
  os.writeObject(obj);
  os.flush();
  byte[] sendBuf = byteStream.toByteArray();
  os.close();
 return sendBuf;

}

 public static String writelogcollection(byte[] bytes) throws IOException {
BufferedReader bf;
    String outStr;
    try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
        bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
        outStr = "";
        String line;
        while ((line = bf.readLine()) != null) {
            outStr += line;
        }
    }
    bf.close();
    return outStr;
}

我正在尝试 GZIPInputStream 但得到以下异常

Exception occurred in target VM: Not in GZIP format 
    java.util.zip.ZipException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:78)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:90)

我确定它不是 java 版本问题,我在代码中缺少它。

【问题讨论】:

  • 您正在尝试使用 GZIPInputStream 读取您刚刚创建的数据,但不是 GZIP 格式。当然它打破了说它是......好吧,不是GZIP格式。也许您打算在 convert() 中使用 GZIPOutputStream?
  • 不,我需要先将我的地图转换为字节数组,然后再对其进行 GZIP 压缩。所以在 convert() 中,我将映射转换为字节数组并将其传递给 writelogcollection()。
  • 但是你实际上在做的是convert() 然后gUNzip它。当然不行。
  • 那么在 Convert() 中应该改变什么,这就是我的查询是什么?
  • 我不明白。如果您需要 GZIP 数据,为什么要尝试 GUNZIP?

标签: java exception gzipinputstream


【解决方案1】:

你的错误在这里:

os.flush();
byte[] sendBuf = byteStream.toByteArray();
os.close();

应该是

os.flush();
os.close();
byte[] sendBuf = byteStream.toByteArray();

因为flush() 只写入到目前为止已经写入的数据,但close() 还写入了一些重要的元数据,这些元数据是正确形成压缩对象流所需的。

你的代码也有不同的小问题,比如writelogcollection应该是writeLogCollection,你应该使用try-with-resources来关闭你的BufferedReader和InputStreamReader(你根本没有关闭它们),以及其他代码清洁问题。保持代码整洁可以让您更容易理解问题所在。

哦,最后但同样重要的是,首先使用ObjectOutputStream 写入对象然后使用BufferedReader 读取它们是没有意义的。 ObjectOutputStream 生成二进制数据,如果您尝试将其解释为文本,则会变成垃圾。

【讨论】:

    猜你喜欢
    • 2011-01-13
    • 2012-07-17
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    相关资源
    最近更新 更多