我正在REST调用中发送文件和一个javascript对象。即时通讯按如下方式进行:
TypeScript Code :
formdata.append('file1', this.filedata);
formdata.append('ctcDetail', JSON.stringify(this.ctcDetailTo));
this.service.submitFuel(formdata).subscribe(
info => {
},
error => this.errorMessage = error);
service.ts :
submitFuel(formdata){
return this.http.post(this.url("url"), formdata, new RequestOptions())
.map((res) => res.json().data.list);
由于formdata仅接受String / blob和File类型,因此我将JS对象转换为String。 现在我将文件检索为byteArray,并且希望将json对象转换为NewCTCDetailTO的实际对象
REST :
@POST
@Path("/url")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public RestResponse<?> submitFuel(MultipartFormDataInput input) throws IOException{
NewCTCDetailTO ctcDetailTO = null;
if (null == input) {
return RestResponse.build().sessionTimeout();
}
Map<String, List<InputPart>> multipartForm = input.getFormDataMap();
List<InputPart> inputParts = multipartForm.get("file1");
InputPart documentPart = inputParts.get(0);
MultivaluedMap<String, String> header = documentPart.getHeaders();
InputStream inputStream = documentPart.getBody(InputStream.class, null);
byte[] bytes = IOUtils.toByteArray(inputStream);
List<InputPart> inputParts1 = multipartForm.get("ctcDetail");
InputPart documentPart1 = inputParts1.get(0);
我在Java代码中可以做什么?