手机版
你好,游客 登录 注册
背景:
阅读新闻

node.js+Android(使用HttpURLConnection和HttpClient)实现文件上传

[日期:2012-02-06] 来源:Linux社区  作者:dead-horse [字体:大 中 小]
别忘了加权限

[html]
  1. <uses-permission Android:name="android.permission.INTERNET"/>  

先看看运行效果



点击Java上传


后台输出


文件上传成功



点击Apache上传


后台输出


文件保存成功


运行一切OK。

解释一下代码:

[java]
  1. private static final String UPLOAD_FILE = "/sdcard/test.jpg";  
  2. private static final String UPLOAD_URL = "http://192.168.9.194:8080/";  

在sd卡根目录上放上test.jpg文件,node.js+android http请求响应 讲过,不要使用localhost。

代码其它部分主要是要了解http文件上传的协议 RFC1867,http://www.ietf.org/rfc/rfc1867.txt IETF官方介绍,它提供的例子不错

6. Examples

   Suppose the server supplies the following HTML:

     <FORM ACTION="http://server.dom/cgi/handle"
           ENCTYPE="multipart/form-data"
           METHOD=POST>
     What is your name? <INPUT TYPE=TEXT NAME=submitter>
     What files are you sending? <INPUT TYPE=FILE NAME=pics>
     </FORM>

   and the user types "Joe Blow" in the name field, and selects a text
   file "file1.txt" for the answer to 'What files are you sending?'

   The client might send back the following data:

        Content-type: multipart/form-data, boundary=AaB03x

        --AaB03x
        content-disposition: form-data; name="field1"

        Joe Blow
        --AaB03x
        content-disposition: form-data; name="pics"; filename="file1.txt"
        Content-Type: text/plain

         ... contents of file1.txt ...
        --AaB03x--

   If the user also indicated an image file "file2.gif" for the answer
   to 'What files are you sending?', the client might client might send
   back the following data:

        Content-type: multipart/form-data, boundary=AaB03x

        --AaB03x
        content-disposition: form-data; name="field1"

        Joe Blow
        --AaB03x
        content-disposition: form-data; name="pics"
        Content-type: multipart/mixed, boundary=BbC04y

        --BbC04y
        Content-disposition: attachment; filename="file1.txt"

如果看不太懂的话,可以在搜索引擎里找 rfc1867实现。 linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款