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

8
我很菜,不是最有经验的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;}
    }
}

zip 是一种压缩格式,您想要压缩还是解压缩? - Roman C
这并不总是那么简单。我知道这是维基百科,但请参阅此处以获取更多信息。 - Jacob Schoen
有趣的是,我刚刚在Windows安全模式下尝试了该程序,它可以正常工作。因此,某些应用程序(病毒扫描器、防火墙等)必须会干扰...有什么方法可以规避这个问题吗?或者强制使用gzip请求? - user1766413
@MiserableVariable:我完全接收到了它,没有编码。我需要相应地调整我的代码吗?怎么做?顺便说一下,谢谢你! - user1766413
显示剩余7条评论
1个回答

3
   connection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch"

您的请求表明它愿意接受以下任何一种编码格式:gzipdeflatesdch。一种方法是查看响应头,以查看服务器使用的编码类型,并适当地解码它。
另一种方法是仅接受gzip

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接