2017-04-10 93 views
0

我正在使用电子来构建一个应用程序,它使用快递在Web服务器中提供多个图像文件。快速检测获取请求

从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()); 
+0

使用类似的console.log(“测试++++++++++++”),而不是警报(),以检查是否请求与服务器的console.log相同 –

+0

取得结果 – takluiper

回答

0

使用此代码,那么你应该使用res.send(),即使用我的编译弃用versión(23),express检测获取请求。

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); 
0

如果调用API的使用JSON

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'); 

      }); 
     }); 
    });