I am trying to upload an FILE (image or pdf, I have not placed a restriction yet) from Angular. to a REST service built in Java, and java I get their data, the original name of the image, the size of the image, but the file is not going up ..
In java, the FormDataMultiPart object should bring the InputStream with the file between its objects. If the FormDataMultiPart arrives, but does not bring in the InputStream, its size comes out of -1
Well, obviously I want to recover the stream, to save that file in BD from Java Any ideas?
This is my Component.ts
//INSERT JUICIO
this.juicio.idStatus = 1;
this._ceJuicioService.insertJuicio(this.juicio).subscribe(
response => {
this.juicio = response;
//subir imagen
if(this.filesToUpload != undefined){
this._uploadService.makeFileRequest(Global.url+"files/"+this.juicio.idJuicio,
[],
this.filesToUpload,
'uploadFile')
.then((result:any) => {
console.log("result");
console.log(result);
form.reset();
});
}
this.formJuicioHabilitado = false;
this.getComponentJuiciobyId(this.juicio.idJuicio);
$('#juicioModal').modal('hide');
$('#juicioGuardadoExitoso').modal('show');
},
error => {
console.log(<any>error);
console.log("No fué posible insertar el Juicio");
$('#juicioModal').modal('hide');
}
);
This is my UploadService:
makeFileRequest(url:string, params:Array<string>, files:Array<File>, name: string){
return new Promise(function(resolve, reject){
var formData:any = new FormData();
var xhr = new XMLHttpRequest();
for(var i=0; i<files.length; i++){
formData.append(name, files[i], files[i].name);
}
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
console.log("status 200");
resolve(JSON.parse(xhr.response));
console.log("final final");
}else{
reject(xhr.response);
}
}
}
xhr.open('POST', url, true);
xhr.send(formData);
});
}
java REST:
@POST
@Path("{idJuicio}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String[enter image description here][1] uploadFile(
FormDataMultiPart form,
@FormDataParam("uploadFile") FormDataContentDisposition fileDetail,
@PathParam("idJuicio") Integer idJuicio){
System.out.println("idJuicio: "+idJuicio);
System.out.println("form: "+form);
System.out.println("fileDetail: "+fileDetail);
if (form == null || fileDetail == null){
System.out.println("Error 400");
return Response.status(400).entity("Invalid form data").build().toString();
}
InputStream fileInput = null;
try{
FormDataBodyPart formUpload = form.getField("uploadFile");
ContentDisposition headerOfFile = formUpload.getContentDisposition();
fileInput = formUpload.getValueAs(InputStream.class);
byte[] targetArray = new byte[fileInput.available()];
fileInput.read(targetArray);
Juicio juicio = new Juicio();
juicio.setIdJuicio(idJuicio);
juicio.setDocumentacionFirmada(targetArray);
idJuicio = JuiciosDaoImpl.updateFileJuicio(juicio);
}catch(Exception e){
e.printStackTrace();
}
return Response.status(200).entity(fileInput+"-"+idJuicio).build().toString();
}