繁体   English   中英

从Android在Python AppEngine上上传并读取文件

[英]Upload from Android on Python AppEngine and read the file

我想将一个csv文件从Android发送到Python AppEngine。 我正在使用Blobstore API并发送文件,我使用MultipartEntityHttpPostHttpGet

因此,根据Blob存储API,则必须调用方法create_upload_url('/upload')生成的URL上传的文件,并使用此URL作为动作要上传的文件。 如你所见

我在做什么? 我调用一个创建该URL的方法,并将其返回到我的android应用。 并使用此网址上传文件。

生成的网址采用以下格式:

myapp.appspot.com/_ah/upload/ 大量字母和字母/

基本上是这样的:

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代码

# Returns only the URL in which the file will be uploaded, so this URL may be used in client for upload the file
class GetBlobstoreUrl(webapp.RequestHandler):
    def get(self):
        logging.info('here')
        upload_url = blobstore.create_upload_url('/upload')
        logging.info("url blob %s", upload_url)
        self.response.out.write(upload_url)

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        logging.info('inside upload handler')
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        return blob_info.key()

def main():
    application = webapp.WSGIApplication([('/upload', UploadHandler), ('/get_blobstore_url', GetBlobstoreUrl)], debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
    main()

问题1

当我将文件发送到AppEngine返回的网址时,这会自动调用服务器方法UploadHandler吗? 因为未显示此方法内的日志消息, 并且正在插入文件 ,并且使用生成的url上传文件时收到的响应是404错误,如果正在上传文件,为什么会出现该错误?

问题2

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

谢谢。

当我将文件发送到AppEngine返回的网址时,这会自动调用服务器方法UploadHandler吗?

正确。

当我将文件发送到AppEngine返回的网址时,这会自动调用服务器方法UploadHandler吗?

向我们显示您的服务器日志-您获得404的网址是什么? 您从上传处理程序中收到任何日志消息吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM