Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

415
Vistas
Upload File from Angular Client to Spring Rest

I am trying to upload a file with Angular and send it to my rest backend Spring. For my angular service, I used the settings here.But I am having 2 issues:

The first one: when I send large zip file: I have this error in my browser: net::ERR_CONNECTION_RESET; it seems that Angular is not able to send the file to the rest client.

The second one: when my zip file is not too big, the first error is bypassed, but this time, I have this error returned from the rest api: 415 (Type de Support Non Supporté).

service Angular

deployement(file: File): void {
    console.log("service angular");
    let formData:FormData = new FormData();
    formData.append('uploadFile', file, file.name);
    let headers = new Headers();
    headers.append('Content-Type', 'multipart/form-data')
    headers.append('Accept', 'application/json');
    let options = new RequestOptions({ headers: headers });
    this.http.post(URL_API_REST + 'upload', formData, options)  
      .map(res => res.json())
      .catch(error => Observable.throw(error))
      .subscribe(
        data => console.log('success'),
        error => console.log(error)
      )
  }

my spring rest

@RequestMapping(value = "upload", headers = ("content-type=mulitpart/*"), method = RequestMethod.POST)
    @ResponseBody
    public HashMap<String, String> uploaderDeploiement(@RequestBody MultipartFile fichierZip){
//java code here
}

Could anyone point out what I am doing wrong?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I think @RequestBody is wrong for file upload.

Try

public HashMap<String, String> uploaderDeploiement(@RequestParam(value = "file", required = true) final MultipartFile file){}

Or

public HashMap<String, String> uploaderDeploiement(MultipartHttpServletRequest request){}

and then get MultipartFile from MultipartHttpServletRequest

over 4 years ago · Santiago Trujillo Denunciar

0

You can refer this code for file upload as its working fine on myside.

java Code

  @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
    public URL uploadFileHandler(@RequestParam("name") String name,
                                 @RequestParam("file") MultipartFile file) throws IOException {

/******* Printing all the possible parameter from @RequestParam *************/

        System.out.println("*****************************");

        System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
        System.out.println("file.getContentType()" + file.getContentType());
        System.out.println("file.getInputStream() " + file.getInputStream());
        System.out.println("file.toString() " + file.toString());
        System.out.println("file.getSize() " + file.getSize());
        System.out.println("name " + name);
        System.out.println("file.getBytes() " + file.getBytes());
        System.out.println("file.hashCode() " + file.hashCode());
        System.out.println("file.getClass() " + file.getClass());
        System.out.println("file.isEmpty() " + file.isEmpty());

        /*************Parameters to b pass to s3 bucket put Object **************/
        InputStream is = file.getInputStream();
        String keyName = file.getOriginalFilename();
}

Angular js

var form = new FormData();
form.append("file", "image.jpeg");

var settings = {
 "async": true,
 "crossDomain": true,
 "url": "http://url/",
 "method": "POST",
 "headers": {
   "cache-control": "no-cache"
 },
 "processData": false,
 "contentType": false,
 "mimeType": "multipart/form-data",
 "data": form
}

$.ajax(settings).done(function (response) {
 console.log(response);
});
over 4 years ago · Santiago Trujillo Denunciar

0

Some entries were missing in my spring configuration files and in my pom.xml.

pom.xml

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

In my dispatcher-servlet.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="536870912"/>
    </bean>

Rest Api

public HashMap<String, String> uploaderDeploiement(@RequestParam MultipartFile file){
//java code here
}

Hope, it will help someone!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda