简体   繁体   English

如何在Java中接收和解压缩gzip数据?

[英]How to receive and uncompress gzip data in java?

I am sending data from my iPhone to server. 我正在从iPhone向服务器发送数据。 Code is following: 代码如下:

      ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]]; 
     [request setShouldCompressRequestBody:YES];   // gzip compression
     [request setTimeOutSeconds:60];
     [request setPostValue:emailString forKey:@"email"]; 
     [request setPostValue:jsonStr forKey:@"jsonstring"];
     [request setPostValue:pwd forKey:@"password"];
     [request setDelegate:self]; 
     [request startAsynchronous];

I tried to search that how can i receive this data in my server but couldn't find any help. 我试图搜索如何在服务器中接收此数据,但找不到任何帮助。 Will i receive it using req.getParameter(). 我会使用req.getParameter()收到它吗? If yes then what will be the parameter name and then how to unzip it and use email, jsonstring, password parameters. 如果是,那么参数名是什么,然后解压缩它并使用email,jsonstring,password参数。 Thanks in advance. 提前致谢。

The easiest way (IMO) to do it in a standard J2EE environment is by using a GZIP compression filter. 在标准J2EE环境中,最简单的方法(IMO)是使用GZIP压缩过滤器。 You can find some example in this question (although this one is only about response filter as far as I can see) or here (this one, I believe, can also do request decompression which you asked about). 您可以在此问题中找到一些示例(尽管据我所知,这仅与响应过滤器有关)或在此处 (我相信,该示例也可以请求您提出的解压缩)。

You then plug such a filter into your webapp by using a <filter> element in your web.xml (see here for an example of how to use servlet filters), thus avoiding any hardcoded dependencies from your webapp's code. 然后,通过在web.xml使用<filter>元素将此类过滤器插入到webapp中(请参见此处 ,了解如何使用servlet过滤器的示例),从而避免了webapp代码中的任何硬编码依赖项。

All you need here is some Servlet container like Tomcat (I don't think you need a whole Java EE environment unless your server applications is more complex, eg is distributed, JSP/Servet subset seems to be enough). 您只需要像Tomcat这样的Servlet容器即可(我认为您不需要整个Java EE环境,除非您的服务器应用程序更复杂,例如是分布式的,JSP / Servet子集就足够了)。 It's a server (both HTTP server and server in general, for any raw data) in essence where JSPs and Servlets do a request handling. 本质上,这是一台服务器(对于任何原始数据,都是HTTP服务器和服务器),其中JSP和Servlet执行请求处理。 By the way, Tomcat has a built-in gzip compression support which is simply configurable. 顺便说一句,Tomcat具有内置的gzip压缩支持,可以简单地对其进行配置。 So it should be easy. 因此,这应该很容易。

Then you need to write a Servlet which extracts all parameters, mentioned in the client iOS code, from your HTTP request and construct and send a response. 然后,您需要编写一个Servlet,该Servlet从HTTP请求中提取客户端iOS代码中提到的所有参数,并构造并发送响应。 (There are a lot of articles, official documentation etc. about servlets) How they're extracted won't depend on whether you use GZIP or not, decompressions should be done before by a server. (有很多关于servlet的文章,官方文档等。)如何提取它们并不取决于您是否使用GZIP,解压缩应该在服务器之前完成。

UPDATE UPDATE

As for parameter names, I guess (I'm not an iOS developer) they are strings right after 'setPostValue' message in your code, like 至于参数名称,我猜(我不是iOS开发人员),它们是代码中“ setPostValue”消息之后的字符串,例如
... ...
[request setPostValue: emailString forKey:@"email"]; [request setPostValue: emailString forKey:@“ email”];
... ...
So, they're currently 所以,他们目前

emailString
jsonStr
pwd

Summary, the reading code should be like this: 总结,阅读代码应该是这样的:

public abstract class MyServlet extends HttpServlet {

  /* (non-Javadoc)
  * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  */
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String emailString = req.getParameter("emailString");
    String jsonStr = req.getParameter("jsonStr");
    String pwd = req.getParameter("pwd");
    //...       
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM