in my system i need to upload a CSV file and save details in the database. As the first step i'm saving the uploaded file in a selected folder.However when i try this i get the following error message.(Error shown in the image). Can someone help me to solve this? Thank you.
subjectController
@Controller
public class SubjectController {
@Autowired
private SubjectDAO subjectDAO;
@Autowired
private CourseDAO courseDAO;
@Autowired
private LectureHallDAO lectureHallDAO;
private final SubjectRepository subjectRepository;
public SubjectController(SubjectRepository subjectRepository) {
this.subjectRepository = subjectRepository;
}
//Save the uploaded file to this folder
private static String UPLOADED_FOLDER = "F://temp//";
@GetMapping("/sub")
public String index() {
return "addAllSubject";
}
@PostMapping("/upload") // //new annotation since 4.3
public String singleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
//redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:uploadStatus";
}
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);
//redirectAttributes.addFlashAttribute("message",
// "You successfully uploaded '" + file.getOriginalFilename() + "'");
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/uploadStatus";
}
@GetMapping("/uploadStatus")
public String uploadStatus() {
System.out.println("error");
return "uploadStatus";
}
}
addAllSubjects html file
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Spring Boot file upload example</h1>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" name="file" /><br/><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>