我正在尝试将文件从我的本地uploads
文件夹上传到外部网络服务器(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。
答案 0 :(得分:0)
要检索文件,请按以下步骤处理:
@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
}