REST fileupload in Spring controller using multipart/form-data - TagMerge
1REST fileupload in Spring controller using multipart/form-dataREST fileupload in Spring controller using multipart/form-data

REST fileupload in Spring controller using multipart/form-data

Asked 1 years ago
0
1 answers

Yes you can pass the other form fields also along with multi part data. You can check the field name and use it like. (if (item.getName().equals("desctiption"))).

try {
    // Parse the request
    List items = upload.parseRequest(request);
    Iterator iterator = items.iterator();
    while (iterator.hasNext()) {
     FileItem item = (FileItem) iterator.next();
     if (!item.isFormField() && !item.getName().equals("")) {
      String fileName = item.getName();
      String root = context.getRealPath("/");
      File path = new File(root + "/uploads");
      if (!path.exists()) {
       boolean status = path.mkdirs();
      }

      File uploadedFile = new File(path + "/" + fileName);
      fileNames.add(fileName);
      System.out.println("File Path:-"
        + uploadedFile.getAbsolutePath());

      item.write(uploadedFile);
     }
    }
   } catch (FileUploadException e) {
    System.out.println("FileUploadException:- " + e.getMessage());
   } catch (Exception e) {
    System.out.println("Exception:- " + e.getMessage());
   }
  }

Source: link

Recent Questions on spring

    Programming Languages