使用Python Bottle服务器接收Android的POST请求中的FileEntity

2 投票
1 回答
981 浏览
提问于 2025-04-17 02:55

在安卓手机上,我使用setEntity()方法把FileEntity放进了POST请求里。

HttpPost post = new HttpPost(uri);
FileEntity reqEntity = new FileEntity(f, "application/x-gzip");
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
post.addHeader("X-AethersNotebook-Custom", configuration.getCustomHeader());
post.setEntity(reqEntity);

但是在使用bottle框架时,我尝试这样做却没有成功。

f = request.body
gzipper = gzip.GzipFile( fileobj= f )
content = gzipper.read()

结果内容是一个空字符串。所以我去查看request.forms和request.files。这两个都没有任何键值。

request.files.keys()
request.forms.keys()

在搜索的时候,我看到关于实体的描述:“一个请求可以传输实体”,而这个实体有实体头和实体值。所以它可能类似于file-content = e.get(entity-header)。

1 个回答

1

使用那段代码,手机通过分块编码发送文件。因为py-bottle不支持分块编码,所以这里的解决办法是重新编写安卓代码,让它把文件作为POST请求的主体发送。

撰写回答