从Android电子通过上传表单文件到GAE [英] Uploading file via form into GAE from Android
问题描述
我在GAE上的应用程序: http://1.myawesomecity.appspot.com/
I have an app on GAE at: http://1.myawesomecity.appspot.com/
固定:
HttpPost post = new HttpPost("http://1.myawesomecity.appspot.com/");
http_client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
String result = EntityUtils.toString( http_client.execute(post).getEntity(), "UTF-8");
String actualURL = result.substring(result.indexOf("http://"), result.indexOf("\" method"));
Log.w("asdf", "url " + actualURL );
post = new HttpPost(actualURL);
http_client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
String mime_type = "image/png";
File file = new File( filename ); //context.getFilesDir(),
entity.addPart( "myFile", new FileBody( file, mime_type));
post.setEntity( entity );
String res = EntityUtils.toString( http_client.execute(post).getEntity(), "UTF-8");
Log.w("asdf", res);
以上抓起从GAE服务器上的实际上传网址,并将所指示由下正确答案的文件中。
The above grabs the ACTUAL upload URL from the GAE server, and passes in the file as dictated by the CORRECT answer below.
老问题:
正如你所看到的,如果你选择一个文件,并点击提交后,将404,但该文件实际上并获取存储(只要不是太大,< 100KB)。不要键入在第一个文本字段什么。
As you can see, if you choose a file and hit submit, it will 404, but the file actually does get stored (as long as it is not too big, < 100kb). Don't type in anything in the first text field.
现在,撇开这个特定的应用程序是如何勉强的功能,我想从Android上的文件上传到该服务器。
Now, putting aside how this particular app is barely functional, I'm trying to upload a file from Android onto this server.
该网站的上传脚本使用Blob存储,文件字段的名称是MYFILE。
The site's upload script uses blobstore, and the file field's name is "myFile".
现在在我的Android应用程序,我有:
Now in my Android app, I have:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(<my app's url>);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myFile", <path to a file selected by user> ) );
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
这抛出异常。
这是怎么从我通过浏览器去我的网站,选择一个文件,然后点击提交什么不同呢?为什么通过浏览器去实际上上传文件,当Android的code不办理什么手续?
How is this any different from me going to my site through a browser, choosing a file, and hitting submit? Why does going through a browser actually go through with uploading the file, when the Android code does not?
我知道我的文件路径是有效的。是不是我做错了吗?或者点击来自于Android执行的HttpClient不同的浏览器提交?
I know that my filepath is valid. Is there something I'm doing wrong? or is clicking on "submit" from a browser different from executing a httpclient from Android?
推荐答案
上传文件到GAE Blob存储区是一个两个步骤:
Uploading file to a blobstore on GAE is a two step process:
-
首先,你需要得到适当的URL在哪里发表您的数据,通常人们用类似/ bloburl处理程序用于这一目的
first you need to get a proper URL where to POST your data, usually people use something like "/bloburl" handler for that purpose
当你有一滴上传网址,您可以使用它在您的要求。
when you have blob upload URL, you use it in your request.
你送不走的的NameValuePair </ code>的文件,它应该去为一个
MultiPartEntity
。
这里的code,工程(你所需要的MultiPartEntry支持的Apache HTTP库):
here's the code that works (you'll need apache http library for MultiPartEntry support):
DefaultHttpClient http_client = new DefaultHttpClient();
HttpGet http_get = new HttpGet(Config.BASE_URL + "bloburl");
HttpResponse response = http_client.execute(http_get);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String first_line = reader.readLine();
Log.w(TAG, "blob_url: " + first_line);
HttpPost post = new HttpPost(first_line);
http_client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
mime_type = "application/zip";
File file = new File( context.getFilesDir(), filename );
entity.addPart( "file", new FileBody( file, mime_type));
post.setEntity( entity );
String result = EntityUtils.toString( http_client.execute(post).getEntity(), "UTF-8");
Log.i(TAG, result);
这篇关于从Android电子通过上传表单文件到GAE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!