使用Ionic本机文件传输插件将File旁边的JSON字符串上传到Java JAX-RS Server

时间:2017-06-13 01:07:06

标签: java angular ionic-framework ionic3

我正在尝试将文件上传到我的Java JAX-RS(Jersey)Web服务。

我的方法签名在服务器端,如果有兴趣的人是这样的话;

upload(@FormDataParam("file") InputStream uploadedStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("question") String req_question, @Context HttpHeaders headers)

当我尝试使用multipart / form-data上传Postman(客户端进行测试)的文件时,它正常工作。当我用Fiddler嗅闻时,我看到了这一点;

POST http://localhost:8080/application/question/post HTTP/1.1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryT79BovKwL6ObBceu
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
------WebKitFormBoundaryT79BovKwL6ObBceu
Content-Disposition: form-data; name="file"; filename="PARS.png"
Content-Type: image/png
[HERE SOME STRANGE RAW BINARY DATA]
------WebKitFormBoundaryT79BovKwL6ObBceu
Content-Disposition: form-data; name="question"
{"title": "This is title!",  "fk_field_id":1}
------WebKitFormBoundaryT79BovKwL6ObBceu--

正如您所看到的,它具有正确的发送数据类型的不同。但是当我尝试使用Ionic / Angular实现相同的上传时,我的请求看起来像这样;

POST http://localhost:8080/application/question/post HTTP/1.1
Content-Type: multipart/form-data
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
/Exif  II*  [SOME STRANGE RAW BINARY DATA]

我只看到二元,我发送的参数没有区别也没有标志。这是我的Angular代码,它被响应为失败。

let options: FileUploadOptions =
 {
   fileKey: 'file',
   headers: headers,
   params: {"title": "This is title!",  "fk_field_id":1} // Actually I am getting this as method parameter yet to shorten code I moved it here
};
path = {MY ENDPOINT IN SERVER}
filePath = file:///storage/emulated/0/Android/data/io.ionic.starter/cache/1497313256081.jpg  // Getting as method param
fileTransfer.upload(filePath, path, options).then(things => {
 //Some console.log
});

0 个答案:

没有答案