编程语言
首页 > 编程语言> > 使用express和multer将带有Java MultipartEntity的映像上传到Node.js服务器

使用express和multer将带有Java MultipartEntity的映像上传到Node.js服务器

作者:互联网

我正在尝试使用自定义MultipartEntity从Android应用程序上传图像,该自定义MultipartEntity也会更新ProgressDialog(这也是我使用已弃用的MultipartEntity的原因).
相关的Java代码:

        File file = new File(imgPath);

        HttpPost post = new HttpPost("http://" + SERVER + "/upload");

        MultipartEntity entity = new MyMultipartEntity(new MyMultipartEntity.ProgressListener()
        {
            public void transferred(long num)
            {
                publishProgress((int) ((num / (float) totalSize) * 100));
                Log.d("DEBUG", num + " - " + totalSize);
            }
        });
        ContentBody cbFile = new FileBody(file, "image/jpeg");
        entity.addPart("source", cbFile);

        totalSize = entity.getContentLength();

        post.setEntity(entity);

        HttpResponse response = client.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();

        if(statusCode == HttpStatus.SC_OK){
            String fullRes = EntityUtils.toString(response.getEntity());
            Log.d("DEBUG", fullRes);

        } else {
            Log.d("DEBUG", "HTTP Fail, Response Code: " + statusCode);
        }

Node.js服务器文件:

var fs = require('fs');

var express = require('express');
var app = express();

var multer = require('multer');
var upload = multer(
    { 
        limits: {
            fieldNameSize: 999999999,
            fieldSize: 999999999
        },
        dest: 'uploads/' }
    );

app.get('/', function(req, res){
    res.send(
        '<form action="/upload" method="post" enctype="multipart/form-data">'+
        '<input type="file" name="source">'+
        '<input type="submit" value="Upload">'+
        '</form>'
    );
});

app.post('/upload', upload.any(), function(req, res){

    console.log(req.files);

    var tmp_path = req.files[0].path;

    var target_path = 'uploads/' + req.files[0].originalname;

    var src = fs.createReadStream(tmp_path);
    var dest = fs.createWriteStream(target_path);
    src.pipe(dest);
    src.on('end', function() { res.send("ok"); });
    src.on('error', function(err) { res.send({error: "upload failed"}); });
});

app.get('/info', function(req, res){
    console.log(__dirname);
    res.send("image upload server: post /upload");
});

app.listen(8080);
console.log('started server on localhost...');

我也尝试使用upload.single()方法(通过表单工作),但这需要字段的名称,我不知道如何/如果我可以添加到Java多部分实体对象.

我目前得到的是请求上的空文件数组和请求体上的源变量,其中包含看起来像图像的原始数据,所以基本上看起来multer根本没有做任何事情.我错过了什么?

谢谢!

解决方法:

我最终使用了Android Ion库 – https://github.com/koush/ion

Java代码:

Ion.with(context)
            .load("http://" + SERVER + ":8080/upload")
            .progressDialog(pd)
            .setMultipartParameter("name", "source")
            .setMultipartFile("image", "image/jpeg", new File(imgPath))
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {

                    //do stuff with result
                }
            });

服务器代码保持不变.

标签:android,multer,java,node-js,express
来源: https://codeday.me/bug/20190829/1758065.html