从节点上传文件到servlet而不保存它

时间:2021-08-21 21:42:58

On my node express server, I am receiving a pdf file. I am using the below code to get the pdf contents from the request

在我的节点快速服务器上,我收到一个pdf文件。我使用以下代码从请求中获取pdf内容

var data = new Buffer('');
request.on('data', function (chunk) {
    data = Buffer.concat([data, chunk]);
});

request.on('end', function() {
    console.log('PDF data is '+JSON.stringify(data));
});

Now that PDF content is available on node, I need to send it as it is to a J2EE server. In order to do that, I am first saving the PDF file in the node server, reading it from the node server and then piping it to request.post (https://github.com/request/request)

既然节点上有PDF内容,我需要将其原样发送到J2EE服务器。为了做到这一点,我首先将PDF文件保存在节点服务器中,从节点服务器读取它,然后将其传送到request.post(https://github.com/request/request)

var req = require('request');

fs.writeFile('abc.pdf', data, 'binary', function(err) {
    if (err) {
        console.log('Error ' + JSON.stringify(err) );  
        throw err;
    } 
    var source = fs.createReadStream('abc.pdf');
    //send our data via POST request
    source.pipe(req.post('http://'+j2ee_host+':'+j2ee_port+'/myjavaapp/Upload')
});

This works fine. However, I feel the part of saving the PDF file on the node server and then reading it is (before posting to the J2EE server using request module) is completely unnecessary, as I am not making any changes to the file.

这很好用。但是,我觉得将PDF文件保存在节点服务器上然后读取它(在使用请求模块发布到J2EE服务器之前)是完全没必要的,因为我没有对文件进行任何更改。

Once I have the PDF contents in 'data' variable, I would like to directly post them to the J2EE server. However, I have not been able to find a way to use the request module to directly post file contents. I have seen some examples related to POST using request module but they refer to formData. In my case, I don't have formData but instead reading the file from request and directly posting it to the J2EE server.

一旦我在'data'变量中有PDF内容,我想直接将它们发布到J2EE服务器。但是,我还没有找到一种方法来使用请求模块直接发布文件内容。我已经看到了一些与使用请求模块的POST相关的示例,但它们引用了formData。在我的例子中,我没有formData,而是从请求中读取文件并直接将其发布到J2EE服务器。

Is there a way to achieve this and avoid the file write and read?

有没有办法实现这一点,避免文件写入和读取?

EDIT

Below is my complete code

以下是我的完整代码

function upload(request, response) {
    var data = new Buffer('');
    request.on('data', function (chunk) {
        data = Buffer.concat([data, chunk]);
    });

    request.on('end', function () {
           fs.writeFile('abc.pdf', data, 'binary', function(err){
           if (err) {
              console.log('Error ' + JSON.stringify(err) );  
              throw err;
           } 
           var source = fs.createReadStream('abc.pdf');                       
           source.pipe(req.post('http://'+j2ee_host+':'+j2ee_port+'/myj2eeapp/Upload'));                
           })
    })  
}

1 个解决方案

#1


1  

You can pipe directly from the data request to the servlet

您可以直接从数据请求传递到servlet

 var req = require('request');
 function upload(request, response) {
    var target = req.post('http://'+j2ee_host+':'+j2ee_port+'/myjavaapp/Upload');
    request.pipe(target);
    target.on('finish', function () { 
      console.log('All done!');
      //send the response or make a completed callback here...
    });
  }

#1


1  

You can pipe directly from the data request to the servlet

您可以直接从数据请求传递到servlet

 var req = require('request');
 function upload(request, response) {
    var target = req.post('http://'+j2ee_host+':'+j2ee_port+'/myjavaapp/Upload');
    request.pipe(target);
    target.on('finish', function () { 
      console.log('All done!');
      //send the response or make a completed callback here...
    });
  }