Estou tendo esse, contudo já realizei todas as configurações q mostra no vídeo, uso a versão do java mais atual. 10+
ProdutosController: { @Autowired private FileSaver fileSaver;
public ModelAndView gravar(MultipartFile sumario, @Valid { String path = fileSaver.write("arquivos-sumario", sumario); produto.setSumarioPath(path); } }
Produto private String sumarioPath; public String getSumarioPath() { return sumarioPath; } public void setSumarioPath(String sumarioPath) { this.sumarioPath = sumarioPath; }
AppWebConfiguration @Bean public MultipartResolver multipartResolver(){ return new StandardServletMultipartResolver(); }
ServletSpringMVC.java @Override protected void customizeRegistration(Dynamic registration) { registration.setMultipartConfig(new MultipartConfigElement("")); }
Criei o FileSaver: package org.casadocodigo.infra;
import java.io.File; import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; @Component public class FileSaver {
@Autowired
private HttpServletRequest request;
public String write(String baseFolder, MultipartFile file) {
try {
String realPath = request.getServletContext().getRealPath("/"+baseFolder);
String path = realPath + "/" + file.getOriginalFilename();
file.transferTo(new File(path));
return baseFolder + "/" + file.getOriginalFilename();
} catch (IllegalStateException | IOException e) {
throw new RuntimeException(e);
}
}
}
form.jsp <form:form action="${ s:mvcUrl('PC#gravar').build() }" method="post" commandName="produto" enctype="multipart/form-data">
Folder criada em : /casadocodigo/src/main/webapp/arquivos-sumario
: erro GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/casadocodigo] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: java.io.IOException: java.io.FileNotFoundException: C:\Users\rafae\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\casadocodigo\arquivos-sumario\0dfc80cf-5884-496b-86ca-043341bc626b.jpg (O sistema não pode encontrar o caminho especificado)] with root cause java.io.FileNotFoundException: C:\Users\rafae\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\casadocodigo\arquivos-sumario\0dfc80cf-5884-496b-86ca-043341bc626b.jpg (O sistema não pode encontrar o caminho especificado)