【问题标题】:How to get image from directory with node.js and send it to the client side to display on android app如何使用 node.js 从目录中获取图像并将其发送到客户端以在 android 应用程序上显示
【发布时间】:2026-01-14 18:10:01
【问题描述】:

我在服务器端使用 node.js,在客户端(android 应用程序)使用 java。在服务器端,我运行查询以从我的数据库中获取数据,其中一列是图像路径。我想使用图像路径来抓取服务器端的实际图像并将其传递给客户端,以便它可以显示在应用程序上。

知道如何做到这一点吗?请帮忙

【问题讨论】:

    标签: javascript android node.js


    【解决方案1】:
    var fs = require('fs');
    function(req,res){
      fs.readFile('image.jpg', function(err, data) {
        if (err) throw err; // Fail if the file can't be read.
          res.writeHead(200, {'Content-Type': 'image/jpeg'});
          res.end(data); // Send the file data to the browser.
      });
    }
    

    【讨论】:

    • 这对我不起作用,尝试读取文件时会引发错误。我什至尝试设置路径,它仍然没有工作
    最近更新 更多