Skip to main content
 首页 » 编程设计

android之使用 Python bottle server 接收来自 Android 的 POST 请求的 FileEntity

2025年05月04日9zdz8207

在 Android 手机上,我使用 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() 

在搜索时,我读到了实体:“请求可以传输实体”并且该实体具有实体 header 和实体值。所以它可能类似于 file-content = e.get(entity-header)。

请您参考如下方法:

使用该代码,手机使用分块编码发送文件。因为py-bottle不支持chunked enconding,这里的解决方案是重写android以发送文件作为POST请求的主体。