1

estoy intentando mandar un excell del Angular2 al SpringBoot.

HTML:

  <form #fileLoad="ngForm" (ngSubmit)="addExcell()">
      <input type="file" id="file" name="file"  class="form-control" (change)="fileChange($event)"  accept=".xlsx">
      <button class="btn btn-sm btn-outline btn-block">Add</button>
   </form> 

Component.ts

Selecciono un excel->

fileChange(event) {
    let fileList: FileList = event.target.files;
    if (fileList.length > 0) {
      let file: File = fileList[0];
      this.formData.append('uploadFile', file, file.name);
    }
  } 

Doy click en enviar ->

 addExcell() {
    const url = "http://localhost:8080/ml/insert";
    this.service.sendFile(url, this.formData).subscribe(data => {
        console.log(data);
      }
    );
  }

En los servicios configuro las cabeceras y lo envío:

sendFile(url, myFile) {
    let headers = new Headers();
    headers.append('Content-Type', 'multipart/form-data');
    headers.append('Accept', 'application/json');

    return this.http2.post(url,   myFile , { headers: headers });
  }

Y para recogerlo con SpringBoot:

@RestController
@RequestMapping(value = { "/ml" })
@CrossOrigin
public class MyFile{

        @RequestMapping(method = RequestMethod.POST, value = "/insert" , headers = "Content-Type= multipart/form-data", produces = MediaType.APPLICATION_JSON_VALUE)
        @ResponseBody
        public String insert(@RequestBody MultipartFile multipart) {

            System.out.println("file" + multipart);
            return "SpringBoot";

        }
}

error SpringBoot:

org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
    at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:831) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2869) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3216) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1137) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_171]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_171]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.27.jar:8.5.27]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_171]

Error en el html :

introducir la descripción de la imagen aquí

8
  • Prueba a comentar esta línea: headers.append('Content-Type', 'multipart/form-data'); Commented el 13 jul. 2018 a las 13:10
  • @PabloLozano esa línea ya estaba puesta.
    – EduBw
    Commented el 16 jul. 2018 a las 6:12
  • Comentar: convertir en comentario. Quería decir que la quitases Commented el 16 jul. 2018 a las 7:36
  • lo quito del angular o de SpringBoot ?
    – EduBw
    Commented el 16 jul. 2018 a las 7:50
  • De Angular: creo que el problema se soluciona si dejas que ese header se meta automáticamente Commented el 16 jul. 2018 a las 7:53

1 respuesta 1

1

Finalmente con la ayuda de PabloLozano se obtuvo la solución:

Angular :

  addExcell() {
    const url = "http://localhost:8080/ml/insert";
    this.service.sendFile(url, this.formData).subscribe(data => {
        console.log(data);
      }
    );
  }

  fileChange(event) {
    let fileList: FileList = event.target.files;
    if (fileList.length > 0) {
      let file: File = fileList[0];
      this.formData.append('uploadFile', file, file.name);
    }
  }

 sendFile(url, myFile) {
    let headers = new Headers();
    headers.append('Accept', 'application/json');
    return this.http2.post(url,   myFile , { headers: headers });
  }

SpringBoot:

@RequestMapping(method = RequestMethod.POST, value = "/insert", headers = "Content-Type= multipart/form-data", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String insert(@RequestParam("uploadFile") MultipartFile multipart) {

        try {

            File destinationFile = new File("C:\\Users\\yo\\Desktop\\fichero.xlsx");
            multipart.transferTo(destinationFile);

            FileInputStream file = new FileInputStream(destinationFile);
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();

            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    System.out.println(cell.getStringCellValue());
                }

                System.out.println("");
            }
            file.close();

        } catch (

        Exception e) {
            e.printStackTrace();
        }

        return "SpringBoot";

    }

Con esto se consigue mandar un Excell desde Angular2 a SpringBoot.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.