【问题标题】:File upload returns 200 but file not uploaded (express to spring)文件上传返回200但文件没有上传(快递到spring)
【发布时间】:2017-05-12 21:36:34
【问题描述】:

我正在尝试将文件从本地 uploads 文件夹上传到外部 Web 服务器 (Java Spring),但我似乎无法让它工作。我收到 200 Ok 状态返回,但当我检查时,文件尚未上传。

这是我的参考代码:

var form = new FormData();

form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_logo', request('http://localhost:8080/' + req.files.file.path));

request({
    url: someDomain + '/proj/new/deliveryAttachment',
    method: "POST",
    headers: {
        'Accept' : 'application/json, text/plain, */*',
        'Content-Type': 'multipart/form-data'
    },
    jar: getJar(),
    qs: {
        pid: req.query.id
    },
    formData: {
        deliveryAttachment: form
    }
}, function (error, response, body) {       
    res.send(body);
});

这里是 Java Spring 控制器:

@RequestMapping(value = "proj/new/deliveryAttachment", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
public String insertDeliveryAttachment(@RequestParam("pid") long pid,
        @RequestParam("deliveryAttachment") MultipartFile file) {
    try {
        DeliveryAttachment a = new DeliveryAttachment(file.getOriginalFilename(), pid);
        ps.insertDeliveryAttachment(a, file.getBytes());
        return String.valueOf(a.id);
    } catch (IOException e) {
        return "-1";
    }
}

任何帮助将不胜感激!

编辑

我已经截屏了控制台日志,它似乎只能通过中间件 API 传递到它从 uploads 文件夹中获取文件的部分。但它不会继续到 request.post。

【问题讨论】:

  • 从共享的信息中很难判断到底是什么问题。我的建议是放置足够的记录器和/或调试以查看是否所有步骤都已执行/参数是否具有预期值。
  • 好的。现在将编辑帖子。

标签: angularjs spring express multipartform-data ng-file-upload


【解决方案1】:

要检索文件,请像这样处理:

@RequestMapping(value = "proj/new/deliveryAttachment", method =RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
public String insertDeliveryAttachment(@RequestParam("pid") long pid,
    @RequestPart("deliveryAttachment") MultipartFile file) // This is the key annotation to use
{
    //Your code here 
}

【讨论】:

    猜你喜欢
    • 2019-12-11
    • 2021-03-01
    • 2023-03-08
    • 1970-01-01
    • 2013-11-12
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    相关资源
    最近更新 更多