我正在使用电子构建一个应用程序,它在一个app服务器上使用express为多个图像文件提供服务。
从另一个在Android中构建的应用程序中,我从服务器获取文件并将文件发布到它。
当Android应用程序发布这些文件时,我没有遇到任何问题:
app.post('/fileupload', function(req, res) {
alert("post");
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
//console.log("Uploading: " + filename);
fstream = fs.createWriteStream(__dirname + '/images/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
res.redirect('back');
});
});
});
但是,当Android应用程序从服务器获取文件时仍然没有成功的检测结果(它获得了文件,但我无法刷新输出屏幕),我正在尝试使用以下代码:
app.use(function (req, res, next) {
alert("get");
next();
});
这个也是:
app.get('/', function (req, res) {
alert("get");
next();
});
我将这些文件放在一个名为映像的目录中:
var express = require('express')
var app = express()
app.use(express.static('images'));
app.listen(3000);
编辑
如果我打开一个具有相同url Android正在获取的浏览器,它将触发事件并显示警报。当Android打开连接时,它为什么不触发呢?我不知道。get请求的Android代码是:
URL url = new URL(sURL);
HttpURLConnection conection = (HttpURLConnection)url.openConnection();
conection.setRequestMethod("GET");
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream inputURL = new BufferedInputStream(url.openStream());
发布于 2017-04-11 09:19:53
使用此代码,即使用我的编译versión (23)表示不赞成,express将检测get请求。
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(sURL.substring(0, sURL.lastIndexOf("/")));
// Execute the request
HttpResponse response;
response = httpclient.execute(httpget);
发布于 2017-04-10 11:37:23
如果您正在调用API,那么应该使用json使用res.send()
app.post('/fileupload', function(req, res) {
alert("post");
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
//console.log("Uploading: " + filename);
fstream = fs.createWriteStream(__dirname + '/images/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
res.send({status:1,data:{}}) //check this response on android side and change/refresh screen on android side if needed
//res.redirect('back');
});
});
});
https://stackoverflow.com/questions/43321892
复制相似问题