Servlet 3.0从android将gzip读取为多部分
如果数据是作为gzip发送的,如何将发送到服务器的数据直接读入部分? 下面是将文件上传到服务器的基本android代码Servlet 3.0从android将gzip读取为多部分,android,gzip,multipart,servlet-3.0,Android,Gzip,Multipart,Servlet 3.0,如果数据是作为gzip发送的,如何将发送到服务器的数据直接读入部分? 下面是将文件上传到服务器的基本android代码 private void sendViaUrlConnection(String urlPost, ArrayList<BasicNameValuePair> pairList, File[] sentfileList) { HttpURLConnection connection = null; GZIPOutputStream g
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 {}
}
private void sendViaUrlConnection(字符串urlPost,
ArrayList pairList,文件[]sentfileList){
HttpURLConnection=null;
GZIPOutputStream gz=null;
DataOutputStream outputStream=null;
OutputStream服务器OutputStream=null;
试一试{
URL=新URL(urlPost);
connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setUseCaches(false);
connection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
connection.setRequestProperty(“用户代理”,
“Android多部分HTTP客户端1.0”);
connection.setRequestProperty(“内容类型”,
“多部分/表单数据;边界=”+边界);
setRequestProperty(“接受编码”、“gzip、deflate”);
setRequestProperty(“接受”、“文本/html、应用程序/xhtml”
+“+xml,application/xml;q=0.9,*/*;q=0.8”);
if(isServerGzip){
setRequestProperty(“内容编码”、“gzip”);
gz=newgzipoutputstream(connection.getOutputStream());
serverOutputStream=gz;
}否则{
outputStream=新的DataOutputStream(
connection.getOutputStream());
serverOutputStream=outputStream;
}
getMultiPartData(pairList、sentfileList、serverOutputStream、,
边界);
serverResponseCode=connection.getResponseCode();
}最后{}
}
如果isServerGzip为false,则servlet会很好地获取数据,但是如果我尝试发送一个gziOutputStream,则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
}
}
}
@WebServlet(“/AddInfo.jsp”)
@MultipartConfig()
公共类AddInfo扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
response.setContentType(“text/html”);
Collection parts=request.getParts();
用于(零件:零件){
//对每个部分都做些什么
}
}
}
我怀疑(在看到您的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中写入多部分头