Witam tworzę aplikację do uploadu obrazów na serwer, korzystam przy tym z książki Spring MVC 4 projektowanie zaawansowanych aplikacji www i mam dwa problemy, z którymi nie mogę sobie poradzić.
1. Problem z wyświetleniem obrazka anonimowego użytkownika.
2.Drugi problem pojawił się gdy chciałem wyświetlić załadowany obrazek w miejsce "anonimowego użytkownika" dostaje błąd
Attribute value must not be null
i teraz już cała aplikacja się nie uruchamia. Proszę o pomoc bo nie mam pojęcia o co z tym chodzi. Pozdrawiam.
błąd: https://github.com/Baron762/Social/blob/master/Attribute%20value%20must%20not%20be%20null
ps. całość jest na github https://github.com/Baron762/Social
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
@ConfigurationProperties(prefix = "upload.pictures")
public class PicturesUploadProperties {
private Resource uploadPath;
private Resource anomuousPicture;
public Resource getAnomuousPicture() {
return anomuousPicture;
}
public void setAnomuousPicture(String anomuousPicture) {
this.anomuousPicture = new DefaultResourceLoader().getResource(anomuousPicture);
}
public Resource getUploadPath() {
return uploadPath;
}
public void setUploadPath(String uploadPath) {
this.uploadPath = new DefaultResourceLoader().getResource(uploadPath);
}
}
@SessionAttributes("picturePath")
@Controller
public class PictureUploadController {
private final Resource picturesDir;
private final Resource anonymousPicture;
@ModelAttribute("picturePath")
public Resource picturePath() {
return anonymousPicture;
}
@Autowired
public PictureUploadController(PicturesUploadProperties uploadProperties) {
this.picturesDir = uploadProperties.getUploadPath();
this.anonymousPicture = uploadProperties.getAnomuousPicture();
}
@RequestMapping("/upload")
public String uploadPage() {
return "file/uploadPage";
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String onUpload(MultipartFile file, RedirectAttributes redirectAttributes, Model model) throws IOException {
if (file.isEmpty() || !isImage(file)) {
redirectAttributes.addFlashAttribute("error", "Nie właściwy plik. Załaduj plik z obrazem");
return "redirect:/upload";
}
Resource picturePath = copyFileToPictures(file);
model.addAttribute("picturePath", picturePath);
copyFileToPictures(file);
return "file/uploadPage";
}
private Resource copyFileToPictures(MultipartFile file) throws IOException {
String fileExtension = getFileExtension(file.getOriginalFilename());
File tempFile = File.createTempFile("pic", fileExtension, picturesDir.getFile());
try (InputStream in = file.getInputStream(); OutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return new FileSystemResource(tempFile);
}
@RequestMapping(value = "/uploadedPicture")
public void getUploadPicture(HttpServletResponse response, @ModelAttribute("picturePath")Path picturePath) throws
IOException {
response.setHeader("Content-type", URLConnection.guessContentTypeFromName(anonymousPicture.getFilename()));
Files.copy(picturePath, response.getOutputStream());
}
private boolean isImage(MultipartFile file) {
return file.getContentType().startsWith("image");
}
private static String getFileExtension(String name) {
return name.substring(name.lastIndexOf("."));
}
(application.properties)
spring.mvc.locale=pl
upload.pictures.uploadPath=file:./pictures
upload.pictures.anonymousPicture=classpath:/images/anonymous.png