在Python AppEngine上从Android上传并读取fi

2024-09-30 16:37:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把一个csv文件从Android发送到Python AppEngine。我使用blobstoreapi,要发送文件,我使用MultipartEntityHttpPost和{}。在

因此,根据Blobstore API,您必须调用方法create_upload_url('/upload')来生成上载文件的url,并将此url用作操作来上载文件。你可以读到here

我在干什么?我调用一个方法来创建这个url并将其返回到我的android应用程序。我用这个网址上传文件。在

生成的url格式如下:

myapp.appspot.com/_ah/upload/a-lot-of-numbers-and-letters/

基本上是这样的:

Android代码

 HttpClient httpClient = new DefaultHttpClient();
 HttpGet httpGet = new HttpGet(mContext.getString("myapp.appspot.com/get_blobstore_url");

 HttpResponse urlResponse = httpClient.execute(httpGet);

 result = EntityUtils.toString(urlResponse.getEntity());

 Uri fileUri = Uri.parse("file:///sdcard/dir/myfile.csv"); // Gets the Uri of the file in the sdcard
 File file = new File(new URI(fileUri.toString())); // Extracts the file from the Uri

 FileBody fileBody = new FileBody(file, "multipart/form-data");

 MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
 entity.addPart("file", fileBody);

 HttpPost httpPost = new HttpPost(result);

 httpPost.setEntity(entity);

 HttpResponse response = httpClient.execute(httpPost);
 response.getStatusLine();

AppEngine代码

^{pr2}$

问题1

当我将文件发送到AppEngine返回的url时,这将自动调用服务器方法UploadHandler?因为这个方法中的日志消息没有显示,并且文件正在被插入,并且我在上传带有生成的url的文件时得到的响应是404错误,如果文件正在上载,为什么会出现这个错误?在

问题2

上传文件后,如何解析服务器中的csv文件并将文件中的所有数据插入到数据存储?在

谢谢。在


Tags: 文件csvthe方法urlnewurifile
1条回答
网友
1楼 · 发布于 2024-09-30 16:37:40

When I send the file to the url the AppEngine returned, this will automatically call the server method UploadHandler?

正确。

When I send the file to the url the AppEngine returned, this will automatically call the server method UploadHandler?

给我们看看你的服务器日志-你得到404的网址是什么?你从上传处理程序得到任何日志消息吗?

After I upload the file, how can I parse the csv file in the server and insert all the data in the file to the datastore?

使用BlobReader API,并将打开的文件传递给python csv模块。

相关问题 更多 >