It's been 2 days without being able to solve this and I see no other recourse here, the problem is that I try to upload the image to server with the java spring framework and place the path of its location in the database, but when sending it I throw an error and really do not know what can be here I leave the method that makes the upload
public static final String TEACHER_UPLOADED_FOLDER = "images/teachers/";
//CREATE{img}
@RequestMapping(value = "/teachers/images", method = RequestMethod.POST, headers = ("content-type=multipart/form-data"))
public ResponseEntity<byte[]> uploadTeacherImage(@RequestParam("idTeacher") Long idTeacher, @RequestParam("file")MultipartFile multipartFile, UriComponentsBuilder uriComponentsBuilder) {
if (idTeacher == null) {
return new ResponseEntity(new CustomErrorType("Please set id_teacher"), HttpStatus.NO_CONTENT);
}
if (multipartFile.isEmpty()) {
return new ResponseEntity(new CustomErrorType("Please select a file to upload"), HttpStatus.NO_CONTENT);
}
Teacher teacher = _teacherService.findById(idTeacher);
if (teacher == null) {
return new ResponseEntity(new CustomErrorType("Teacher with id_teacher: " + idTeacher + " not found"), HttpStatus.NOT_FOUND);
}
if (!teacher.getAvatar().isEmpty() || teacher.getAvatar() != null) {
String fileName = teacher.getAvatar();
Path path = Paths.get(fileName);
File f = path.toFile();
if (f.exists()) {
f.delete();
}
}
try {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateName = dateFormat.format(date);
String fileName = String.valueOf(idTeacher)
+ "-pictureTaecher-" + dateName + "."
+ multipartFile.getContentType().split("/")[1];
teacher.setAvatar(TEACHER_UPLOADED_FOLDER + fileName);
byte[] image = multipartFile.getBytes();
Path path = Paths.get(TEACHER_UPLOADED_FOLDER + fileName);
Files.write(path, image);//****AQUI ME ARROJA EL ERROR****
_teacherService.update(teacher);
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity(
new CustomErrorType("Error during upload: " + multipartFile.getOriginalFilename()),
HttpStatus.CONFLICT
);
}
}
I throw the error in this part of the code:
byte[] image = multipartFile.getBytes();
Path path = Paths.get(TEACHER_UPLOADED_FOLDER + fileName);
Files.write(path, image);//****AQUI ME ARROJA EL ERROR****
Here is the teacher object:
@Entity
@Table(name = "teacher")
public class Teacher implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idTeacher;
@Column(name = "name")
private String name;
@Column(name = "avatar")
private String avatar;
@OneToMany(mappedBy = "teacher")
@JsonIgnore
private Set<Course> courses;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "id_teacher")
@JsonIgnore
private Set<TeacherSocialMedia> teacherSocialMedia;
public Teacher () { }
public Teacher (String name, String avatar) {
this.name = name;
this.avatar = avatar;
}
public Long getIdTeacher () {
return idTeacher;
}
public void setIdTeacher (Long idTeacher) {
this.idTeacher = idTeacher;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getAvatar () {
return avatar;
}
public void setAvatar (String avatar) {
this.avatar = avatar;
}
public Set<Course> getCourses () {
return courses;
}
public void setCourses (Set<Course> courses) {
this.courses = courses;
}
public Set<TeacherSocialMedia> getTeacherSocialMedia () {
return teacherSocialMedia;
}
public void setTeacherSocialMedia (Set<TeacherSocialMedia> teacherSocialMedia) {
this.teacherSocialMedia = teacherSocialMedia;
}
}
The error that throws me is the following: Line 197 of TeacherController is within the method that is above
java.nio.file.NoSuchFileException: images/teachers/2-pictureTaecher-2018-09-27.png
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:215)
at java.base/java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
at java.base/java.nio.file.Files.newOutputStream(Files.java:218)
at java.base/java.nio.file.Files.write(Files.java:3355)
at com.platzi.profesoresplatzi.controller.TeacherController.uploadTeacherImage(TeacherController.java:197)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)