FileSaver
@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 (Exception e) {
throw new RuntimeException(e);
}
}
}
Classe ServletSpringMVC
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(new MultipartConfigElement(""));
}
Classe ProdutoController
@RequestMapping(method=RequestMethod.POST)
public ModelAndView gravar(MultipartFile sumario, @Valid Produto produto, BindingResult result, RedirectAttributes redirectAttributes){
if(result.hasErrors()){
return form(produto);
}
String path = fileSaver.write("arquivos-sumario", sumario);
produto.setSumarioPath(path);
produtoDao.gravar(produto);
redirectAttributes.addFlashAttribute("sucesso","Produto cadastrado com sucesso");
return new ModelAndView("redirect:produtos");
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView listar() {
List<Produto> produtos = produtoDao.listar();
ModelAndView modelAndView = new ModelAndView("produtos/lista");
modelAndView.addObject("produtos", produtos);
return modelAndView;
}
Erro:
jun 26, 2017 9:09:03 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
ADVERTÊNCIA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:casadocodigo' did not find a matching property.
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Server version: Apache Tomcat/9.0.0.M21
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Server built: May 4 2017 22:42:36 UTC
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Server number: 9.0.0.0
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: OS Name: Windows 7
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: OS Version: 6.1
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Architecture: amd64
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Java Home: C:\Program Files\Java\jre1.8.0_131
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: JVM Version: 1.8.0_131-b11
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: JVM Vendor: Oracle Corporation
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: CATALINA_BASE: C:\Desenvolvimento\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: CATALINA_HOME: C:\Desenvolvimento\Server\apache-tomcat-9.0.0.M21
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Dcatalina.base=C:\Desenvolvimento\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Dcatalina.home=C:\Desenvolvimento\Server\apache-tomcat-9.0.0.M21
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Dwtp.deploy=C:\Desenvolvimento\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Djava.endorsed.dirs=C:\Desenvolvimento\Server\apache-tomcat-9.0.0.M21\endorsed
jun 26, 2017 9:09:03 AM org.apache.catalina.startup.VersionLoggerListener log
INFORMAÇÕES: Command line argument: -Dfile.encoding=Cp1252
jun 26, 2017 9:09:03 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMAÇÕES: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_131\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_131/bin/server;C:/Program Files/Java/jre1.8.0_131/bin;C:/Program Files/Java/jre1.8.0_131/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\dotnet\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files\Git\cmd;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Windows\system32;;.]
jun 26, 2017 9:09:04 AM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler ["http-nio-8080"]
jun 26, 2017 9:09:04 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMAÇÕES: Using a shared selector for servlet write/read
jun 26, 2017 9:09:04 AM org.apache.coyote.AbstractProtocol init
INFORMAÇÕES: Initializing ProtocolHandler ["ajp-nio-8009"]
jun 26, 2017 9:09:04 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMAÇÕES: Using a shared selector for servlet write/read
jun 26, 2017 9:09:04 AM org.apache.catalina.startup.Catalina load
INFORMAÇÕES: Initialization processed in 944 ms
jun 26, 2017 9:09:04 AM org.apache.catalina.core.StandardService startInternal
INFORMAÇÕES: Starting service [Catalina]
jun 26, 2017 9:09:04 AM org.apache.catalina.core.StandardEngine startInternal
INFORMAÇÕES: Starting Servlet Engine: Apache Tomcat/9.0.0.M21
jun 26, 2017 9:09:06 AM org.apache.jasper.servlet.TldScanner scanJars
INFORMAÇÕES: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
jun 26, 2017 9:09:06 AM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Spring WebApplicationInitializers detected on classpath: [br.com.casadocodigo.loja.conf.ServletSpringMVC@7fbf26fc]
log4j:WARN No appenders could be found for logger (br.com.casadocodigo.loja.conf.ServletSpringMVC).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
jun 26, 2017 9:09:07 AM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Initializing Spring FrameworkServlet 'dispatcher'
jun 26, 2017 9:09:12 AM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["http-nio-8080"]
jun 26, 2017 9:09:12 AM org.apache.coyote.AbstractProtocol start
INFORMAÇÕES: Starting ProtocolHandler ["ajp-nio-8009"]
jun 26, 2017 9:09:12 AM org.apache.catalina.startup.Catalina start
INFORMAÇÕES: Server startup in 8254 ms
jun 26, 2017 9:09:12 AM org.apache.catalina.core.StandardWrapperValve invoke
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:\Desenvolvimento\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\casadocodigo\arquivos-sumario\windows.jpg (O sistema não pode encontrar o caminho especificado)] with root cause
java.io.FileNotFoundException: C:\Desenvolvimento\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\casadocodigo\arquivos-sumario\windows.jpg (O sistema não pode encontrar o caminho especificado)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:394)
at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:120)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.transferTo(StandardMultipartHttpServletRequest.java:258)
at br.com.casadocodigo.loja.infra.FileSaver.write(FileSaver.java:23)
at br.com.casadocodigo.loja.controller.ProdutosController.gravar(ProdutosController.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1366)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
jun 26, 2017 9:14:09 AM org.apache.catalina.core.StandardContext reload
INFORMAÇÕES: Reloading Context with name [/casadocodigo] has started
jun 26, 2017 9:14:09 AM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Destroying Spring FrameworkServlet 'dispatcher'
jun 26, 2017 9:14:09 AM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
ADVERTÊNCIA: The web application [casadocodigo] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
jun 26, 2017 9:14:10 AM org.apache.jasper.servlet.TldScanner scanJars
INFORMAÇÕES: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
jun 26, 2017 9:14:10 AM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Spring WebApplicationInitializers detected on classpath: [br.com.casadocodigo.loja.conf.ServletSpringMVC@60b623f0]
log4j:WARN No appenders could be found for logger (br.com.casadocodigo.loja.conf.ServletSpringMVC).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
jun 26, 2017 9:14:10 AM org.apache.catalina.core.ApplicationContext log
INFORMAÇÕES: Initializing Spring FrameworkServlet 'dispatcher'
jun 26, 2017 9:14:15 AM org.apache.catalina.core.StandardContext reload
INFORMAÇÕES: Reloading Context with name [/casadocodigo] is completed