歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> node.js+Android(使用HttpURLConnection和HttpClient)實現文件上傳

node.js+Android(使用HttpURLConnection和HttpClient)實現文件上傳

日期:2017/3/1 10:33:54   编辑:Linux編程

這篇node.js 第三方模塊如何安裝(使用npm)及介紹 了formidable的安裝(見http://www.linuxidc.com/Linux/2012-02/53531.htm),這一篇結合Android寫一個文件上傳的例子。

先上服務端node.js 的代碼,保存為upload.js

[javascript]

  1. var http = require('http');
  2. var fs = require('fs');
  3. var formidable = require('formidable');
  4. var firstPage = function(res){
  5. res.writeHead(200, {'Content-Type': 'text/html'});
  6. var html = fs.readFileSync(__dirname + '/public/form.html');
  7. res.end(html);
  8. }
  9. var resultPage = function(res,data,files){
  10. res.setHeader('Content-Type', 'text/html');
  11. res.write('<p>thanks ' + data.name + '</p>');
  12. res.write('<ul>');
  13. console.log(data);
  14. console.log(files);
  15. if (Array.isArray(files.images)) {
  16. files.images.forEach(function(image){
  17. var kb = image.size / 1024 | 0;
  18. res.write('<li>uploaded ' + image.name + ' ' + kb + 'kb</li>');
  19. });
  20. } else {
  21. var image = files.images;
  22. var kb = image.size / 1024 | 0;
  23. res.write('<li>uploaded ' + image.name + ' ' + kb + 'kb</li>');
  24. }
  25. res.end('</ul>');
  26. }
  27. var server = http.createServer(function(req, res) {
  28. if (req.method == 'GET'){
  29. return firstPage(res);
  30. }
  31. var form = new formidable.IncomingForm;
  32. var data = {};
  33. var files = {};
  34. form.uploadDir = __dirname +'/file';
  35. form.keepExtensions = true;
  36. function ondata(name, val, data){
  37. if (Array.isArray(data[name])) {//數組
  38. data[name].push(val);
  39. } else if (data[name]) {//同key
  40. data[name] = [data[name], val];
  41. } else {//第一次
  42. data[name] = val;
  43. }
  44. }
  45. form.on('field', function(name, val){
  46. ondata(name, val, data);
  47. });
  48. form.on('file', function(name, val){
  49. ondata(name, val, files);
  50. });
  51. form.on('end', function() {
  52. resultPage(res,data,files);
  53. });
  54. form.parse(req);
  55. });
  56. server.listen(8080);
  57. console.log('listening on http://localhost:8080');
__dirname + '/public/form.html,在js當前目錄下建立public文件夾,文件夾下建立form.html文件,文件內容如下

[html]

  1. <html>
  2. <body>
  3. <form action="/" method="post" enctype="multipart/form-data">
  4. <input type="text" name="name" placeholder="Name:" />
  5. <input type="file" name="images" multiple="multiple" />
  6. <input type="submit" value="Upload" />
  7. </form>
  8. </body>
  9. </html>

代碼比較簡單,formidable部分可以上官網https://github.com/felixge/node-formidable看API。

運行以下看看


在浏覽器中打開http://localhost:8080

選擇文件,輸入文件name

點擊upload


後台截圖


文件上傳成功


再看android代碼,布局文件main.xml如下

[html]

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/RelativeLayout1"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:id="@+id/textViewInfo"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentLeft="true"
  12. android:layout_alignParentTop="true"/>
  13. <TextView
  14. android:id="@+id/textView1"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentLeft="true"
  18. android:layout_below="@+id/textViewInfo"
  19. android:layout_marginLeft="40dp"
  20. android:layout_marginTop="20dp"
  21. android:text="文件路徑"
  22. android:textAppearance="?android:attr/textAppearanceMedium" />
  23. <TextView
  24. android:id="@+id/textViewFile"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_alignLeft="@+id/textView1"
  28. android:layout_below="@+id/textView1"/>
  29. <TextView
  30. android:id="@+id/textView3"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_alignLeft="@+id/textViewFile"
  34. android:layout_below="@+id/textViewFile"
  35. android:layout_marginTop="10dp"
  36. android:text="上傳網址"
  37. android:textAppearance="?android:attr/textAppearanceMedium" />
  38. <TextView
  39. android:id="@+id/textViewUrl"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_alignLeft="@+id/textView3"
  43. android:layout_below="@+id/textView3"/>
  44. <WebView
  45. android:id="@+id/webViewResult"
  46. android:layout_width="fill_parent"
  47. android:layout_height="100dp"
  48. android:layout_below="@+id/textViewUrl"
  49. android:layout_marginTop="30dp"/>
  50. <Button
  51. android:id="@+id/buttonJava"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:layout_alignParentBottom="true"
  55. android:layout_marginLeft="60dp"
  56. android:text="Java上傳" />
  57. <Button
  58. android:id="@+id/buttonApache"
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:layout_alignBottom="@+id/buttonJava"
  62. android:layout_toRightOf="@+id/buttonJava"
  63. android:text="Apache上傳" />
  64. </RelativeLayout>
Copyright © Linux教程網 All Rights Reserved