How to receive and uncompress gzip data in java?

1.4k views Asked by At

I am sending data from my iPhone to server. 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(). If yes then what will be the parameter name and then how to unzip it and use email, jsonstring, password parameters. Thanks in advance.

2

There are 2 answers

0
Seramme On

The easiest way (IMO) to do it in a standard J2EE environment is by using a GZIP compression filter. 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.

0
Wizart On

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, e.g. is distributed, JSP/Servet subset seems to be enough). 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. By the way, Tomcat has a built-in gzip compression support which is simply configurable. 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. (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.

UPDATE

As for parameter names, I guess (I'm not an iOS developer) they are strings right after 'setPostValue' message in your code, like
...
[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");
    //...       
  }
}