我想使用JPG文件形式的参数命令发送到POST请求。
我在4.4.1版本中使用HttpClient
部分Java代码如下所示:
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
File file = new File("path_to_jpg");
HttpPost post = new HttpPost("http://localhost:1337/uploadJPG");
FileBody fileBody = new FileBody(file);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = httpclient.execute(post);
System.out.println(response.getEntity().getContent());
} finally {
httpclient.close();
}
下一步“http://localhost:1337/uploadJPG”想让nodeJS拥有一个处理JPG文件的服务器
服务器代码nodeJS的想法:
var http = require('http'),
fs = require('fs'),
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
//process file JPG
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('processed JPG');
}
});
port = 1337;
host = '127.0.0.1';
server.listen(1337, '127.0.0.1');
console.log('Listening at http://' + '127.0.0.1' + ':' + 1337);
现在我的问题是,如何在NodeJS中创建这样的服务,该文件将以jpg格式存在?