Servlet 3.0 read gzip as multipart from android

怎甘沉沦 提交于 2019-12-22 12:46:28

问题


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:


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.



来源:https://stackoverflow.com/questions/14630255/servlet-3-0-read-gzip-as-multipart-from-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!