Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

api to accept a csv file spring boot

package com.bezkoder.spring.files.csv.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.bezkoder.spring.files.csv.service.CSVService;
import com.bezkoder.spring.files.csv.helper.CSVHelper;
import com.bezkoder.spring.files.csv.message.ResponseMessage;
import com.bezkoder.spring.files.csv.model.Tutorial;
@CrossOrigin("http://localhost:8081")
@Controller
@RequestMapping("/api/csv")
public class CSVController {
  @Autowired
  CSVService fileService;
  @PostMapping("/upload")
  public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") MultipartFile file) {
    String message = "";
    if (CSVHelper.hasCSVFormat(file)) {
      try {
        fileService.save(file);
        message = "Uploaded the file successfully: " + file.getOriginalFilename();
        return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
      } catch (Exception e) {
        message = "Could not upload the file: " + file.getOriginalFilename() + "!";
        return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
      }
    }
    message = "Please upload a csv file!";
    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseMessage(message));
  }
  @GetMapping("/tutorials")
  public ResponseEntity<List<Tutorial>> getAllTutorials() {
    try {
      List<Tutorial> tutorials = fileService.getAllTutorials();
      if (tutorials.isEmpty()) {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
      }
      return new ResponseEntity<>(tutorials, HttpStatus.OK);
    } catch (Exception e) {
      return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Lists - removing 
Java :: reset a jTable without deleting rows 
Java :: Artemis agent/client auto failover 
Java :: Sample TreeMap 
Java :: mock stream java 
Java :: java accessing static variables from event handler 
Java :: Java Default capacity and load factor 
Java :: java ultimo dia del mes 
Java :: Detect a face using OpenCV in Java 
Java :: how to add a note in java 
Java :: android httpurlconnection post multipart/form-data 
Java :: test function that call a function javascrip 
Java :: sudo visudo quit without saving 
Java :: android code get arrayList index 
Java :: show all debug points intellij 
Java :: sudoku 6x6 java 
Java :: function compose method java 8 
Java :: document inserted succesfully but not present in the collection java 
Java :: does not have a NavController set on 21312310 kotlin 
Java :: JAXRS EXCEPTION MAPPER 
Java :: dequeue element to queue java 
Java :: itext new page 
Java :: firemonkey android ini file read 
Java :: java set get all not containing 
Java :: plantuml java 
Java :: += operator casting in java 
Java :: Run the ls command in the terminal to see the uncompiled .java file 
Java :: how to put array in array list stack overflow 
Java :: java intercambiar la posicion de valores de un array 
Java :: Java Multi-catch block 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =