簡體   English   中英

java.util.zip.ZIPException: 不是 GZIP 格式

[英]java.util.zip.ZIPException: Not in GZIP format

我是最有經驗的 JAVA 用戶,但是,我對我的問題非常絕望。 每次執行以下代碼時,都會收到以下錯誤:

 java.util.zip.ZipException: Not in GZIP format
 at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
 at java.util.zip.GZIPInputStream.(init)(Unknown Source)
 at java.util.zip.GZIPInputStream.(init)(Unknown Source)
 at DidYouMean.executeGet(DidYouMean.java:56)
 at DidYouMean.didYouMean(DidYouMean.java:11)
 at DidYouMean.main(DidYouMean.java:39)
 Exception in thread "main" java.lang.IllegalArgumentException: String input must not be null....

我的一個朋友(使用 Mac,而不是我使用 Windows 7 64)能夠執行該程序。 所以這似乎不是代碼本身的問題(無論如何它是由 Github 上的某個人開發的)。 我真的很感激任何幫助! 我對解決方案的搜索並不是很成功,盡管該錯誤並不罕見。

import java.io.*;
import java.net.*;
import org.jsoup.*;
import java.util.zip.*;
import org.jsoup.nodes.*;
import org.jsoup.examples.HtmlToPlainText;
public class DidYouMean {
    public static String didYouMean(String s){
        String word="";
        String url="http://www.google.co.in/search?hl=en&q="+URLEncoder.encode(s);
        String html=executeGet(url,"www.google.co.in",'i');
        Document content=Jsoup.parse(html);
        Element submitted=null;
        try{
            submitted=content.getElementById("topstuff").clone();
            HtmlToPlainText h=new HtmlToPlainText();
            word=h.getPlainText(submitted);
            int q,p=word.indexOf("Did you mean:");
            if(p>=0){
                word=word.substring(p+"Did you mean:".length());
                p=word.indexOf("<>");
                if(p>0) word=word.substring(0,p);
                word=word.trim();
            }
            else{
                p=word.indexOf("Showing results for");
                if(p>=0){
                    word=word.substring(p+"Showing results for".length());
                    p=word.indexOf("<>");
                    if(p>0) word=word.substring(0,p);
                    word=word.trim();
                }
                else return "No results";
            }
        }catch(Exception e){e.printStackTrace();}   
        return word;
    }
    public static void main(String args[]){
        System.out.println(didYouMean(args[0]));
    }
    public static String executeGet(String targetURL,String host,char ch){
        URL url;
        HttpURLConnection connection=null;  
        try{
          url=new URL(targetURL);
          connection=(HttpURLConnection)url.openConnection();
          connection.setRequestMethod("GET");
          connection.setRequestProperty("Host",host);
          connection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
          connection.setRequestProperty("Accept-Language","en-US,en;q=0.8");
          if(ch=='c') connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5");
          if(ch=='i') connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; ShopperReports 3.1.22.0; SRS_IT_E879047EB0765B5336AF90)");
          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);
          GZIPInputStream gzis=new GZIPInputStream(connection.getInputStream());
          InputStreamReader reader=new InputStreamReader(gzis);
          BufferedReader in=new BufferedReader(reader);
          String line;
          StringBuffer response=new StringBuffer(); 
          while((line=in.readLine())!=null) {
              response.append(line);
              response.append('\r');
          }
          in.close();
          return response.toString();
        } catch (Exception e) {e.printStackTrace();return null;}
    }
}
   connection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch"

您的請求表示願意接受以下任何編碼格式: gzipdeflatesdch 一種方法是查看response-headers以查看服務器使用什么類型的編碼並對其進行適當的解碼。

另一種方法是只接受gzip

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM