node.js+Android(使用HttpURLConnection和HttpClient)实现文件上
先上服务端node.js 的代码,保存为upload.js
- var http = require('http');
- var fs = require('fs');
- var formidable = require('formidable');
- var firstPage = function(res){
- res.writeHead(200, {'Content-Type': 'text/html'});
- var html = fs.readFileSync(__dirname + '/public/form.html');
- res.end(html);
- }
- var resultPage = function(res,data,files){
- res.setHeader('Content-Type', 'text/html');
- res.write('<p>thanks ' + data.name + '</p>');
- res.write('<ul>');
- console.log(data);
- console.log(files);
- if (Array.isArray(files.images)) {
- files.images.forEach(function(image){
- var kb = image.size / 1024 | 0;
- res.write('<li>uploaded ' + image.name + ' ' + kb + 'kb</li>');
- });
- } else {
- var image = files.images;
- var kb = image.size / 1024 | 0;
- res.write('<li>uploaded ' + image.name + ' ' + kb + 'kb</li>');
- }
- res.end('</ul>');
- }
- var server = http.createServer(function(req, res) {
- if (req.method == 'GET'){
- return firstPage(res);
- }
- var form = new formidable.IncomingForm;
- var data = {};
- var files = {};
- form.uploadDir = __dirname +'/file';
- form.keepExtensions = true;
- function ondata(name, val, data){
- if (Array.isArray(data[name])) {//数组
- data[name].push(val);
- } else if (data[name]) {//同key
- data[name] = [data[name], val];
- } else {//第一次
- data[name] = val;
- }
- }
- form.on('field', function(name, val){
- ondata(name, val, data);
- });
- form.on('file', function(name, val){
- ondata(name, val, files);
- });
- form.on('end', function() {
- resultPage(res,data,files);
- });
- form.parse(req);
- });
- server.listen(8080);
- console.log('listening on http://localhost:8080');
- <html>
- <body>
- <form action="/" method="post" enctype="multipart/form-data">
- <input type="text" name="name" placeholder="Name:" />
- <input type="file" name="images" multiple="multiple" />
- <input type="submit" value="Upload" />
- </form>
- </body>
- </html>
代码比较简单,formidable部分可以上官网https://github.com/felixge/node-formidable看API。
运行以下看看
在浏览器中打开http://localhost:8080
选择文件,输入文件name
点击upload
后台截图
文件上传成功
再看android代码,布局文件main.xml如下
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RelativeLayout1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/textViewInfo"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"/>
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/textViewInfo"
- android:layout_marginLeft="40dp"
- android:layout_marginTop="20dp"
- android:text="文件路径"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- <TextView
- android:id="@+id/textViewFile"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/textView1"
- android:layout_below="@+id/textView1"/>
- <TextView
- android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/textViewFile"
- android:layout_below="@+id/textViewFile"
- android:layout_marginTop="10dp"
- android:text="上传网址"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- <TextView
- android:id="@+id/textViewUrl"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/textView3"
- android:layout_below="@+id/textView3"/>
- <WebView
- android:id="@+id/webViewResult"
- android:layout_width="fill_parent"
- android:layout_height="100dp"
- android:layout_below="@+id/textViewUrl"
- android:layout_marginTop="30dp"/>
- <Button
- android:id="@+id/buttonJava"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_marginLeft="60dp"
- android:text="Java上传" />
- <Button
- android:id="@+id/buttonApache"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBottom="@+id/buttonJava"
- android:layout_toRightOf="@+id/buttonJava"
- android:text="Apache上传" />
- </RelativeLayout>
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20