【问题标题】:Servlet 3.0 read gzip as multipart from androidServlet 3.0 从 android 读取 gzip 作为 multipart
【发布时间】:2013-01-15 19:38:28
【问题描述】:

如果数据是作为 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,则 servlet 可以正常获取数据,但如果我尝试发送 GZIPOutputStream,则 servlet 中的 getParts() 函数会返回一个空列表。 这是servlet的代码:

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

【问题讨论】:

    标签: android gzip multipart servlet-3.0


    【解决方案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);
    

    here

    请参阅我的问题here,了解管理发送数据的类。请注意,我将连接的输出流包装在 GZIPOutputStream 中,但我没有在 GZIPOutputStream 中写入多部分标头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      相关资源
      最近更新 更多