Servlet 3.0 read gzip as multipart from android

2019-05-21 00:32发布

How Can I read a data sent to a server directly into parts if the data was sent as a gzip? Here is the basic android code that uploads the file(s) to the server

private void sendViaUrlConnection(String urlPost,
        ArrayList<BasicNameValuePair> pairList, File[] sentfileList) {
    HttpURLConnection connection = null;
    GZIPOutputStream gz = null;
    DataOutputStream outputStream = null;
    OutputStream serverOutputStream = null;
    try {
        URL url = new URL(urlPost);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("User-Agent",
            "Android Multipart HTTP Client 1.0");
        connection.setRequestProperty("Content-Type",
            "multipart/form-data; boundary=" + boundary);
        connection.setRequestProperty("accept-encoding", "gzip,deflate");
        connection.setRequestProperty("accept","text/html,application/xhtml"
                    + "+xml,application/xml;q=0.9,*/*;q=0.8");
        if (isServerGzip) {
            connection.setRequestProperty("Content-Encoding", "gzip");
            gz = new GZIPOutputStream(connection.getOutputStream());
            serverOutputStream = gz;
        } else {
            outputStream = new DataOutputStream(
                    connection.getOutputStream());
            serverOutputStream = outputStream;
        }
        getMultiPartData(pairList, sentfileList, serverOutputStream,
            boundary);
        serverResponseCode = connection.getResponseCode();
    } finally {}
}  

If isServerGzip is false the servlet gets the data fine but if I try to send a GZIPOutputStream then the getParts() function in the servlet returns an empty list. here is the servlet's code:

@WebServlet("/AddInfo.jsp")
@MultipartConfig()
public class AddInfo extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            // Do something with each part
        }
    }
}

1条回答
虎瘦雄心在
2楼-- · 2019-05-21 01:04

I suspect (can't be sure till I see your getMultiPartData()) that you are passing to the GZIPOutputStream the multipart headers - things like :

writer.append("--" + boundary).append(CRLF);
writer.append(
        "Content-Disposition: form-data; name=\"binaryFile\"; filename=\""
            + file.getName() + "\"").append(CRLF);
writer.append(
        "Content-Type: "
            + ((isServerGzip) ? "application/gzip" : URLConnection
                    .guessContentTypeFromName(file.getName())))
            .append(CRLF);

see here

See my question here for a class that manages to send the data. Notice I wrap the connection's output stream inside the GZIPOutputStream but I do not write the multipart headers in the GZIPOutputStream.

查看更多
登录 后发表回答