Сервлет 3.0 читает gzip как составной из Android

Как я могу прочитать данные, отправленные на сервер, напрямую по частям, если данные были отправлены в виде gzip? Вот основной код Android, который загружает файлы на сервер.

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 {}
}  

Если isServerGzip имеет значение false, сервлет получает данные в порядке, но если я попытаюсь отправить GZIPOutputStream, то функция getParts() в сервлете вернет пустой список. вот код сервлета:

@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
        }
    }
}

person user2029558    schedule 31.01.2013    source источник


Ответы (1)


Я подозреваю (не могу быть уверен, пока не увижу ваш getMultiPartData()), что вы передаете GZIPOutputStream составные заголовки - такие вещи, как:

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);

см. здесь

См. мой вопрос здесь для класса, которому удается отправить данные. Обратите внимание, что я оборачиваю поток вывода соединения в GZIPOutputStream, но я не записываю составные заголовки в GZIPOutputStream.

person Mr_and_Mrs_D    schedule 17.09.2013