Upload with Thymeleaf

Asked

Viewed 229 times

0

Good afternoon, you guys, I am with a difficulty, I am developing a simple system, it reads a csv file and imports to a database, I risked using Thymeleaf, I found some tutorials but I could not understand very well, if someone knows how to do, please give me a light kkk I was trying to do so at the front:

    <form th:action="@{/upload}" enctype="application/x-www-form-urlencoded">
    <input type="file" name="file" id="file"/></br>
    <input type="submit" name="submit" value="Importar">
</form>

and in the backend:

@GetMapping("/upload")
public void importar(MultipartFile mf) {
    try {
        importService.montaAbastecimento(mf);
    } catch (Exception e) {
        System.out.println("Erro ao importar: " + e.getMessage());
    }

}

this is just the method I want to call, the class is annotated with @Controller

  • And what’s the problem?

  • I don’t know much about Thymeleaf, but if backend you expect an object MultipartFile, you shouldn’t send a Multipart also?

  • @nullptr the problem is this Thymeleaf template Might not exist or Might not be accessible by any of the configured Template Resolvers

  • add the stacktrace to the question

  • @Andersoncarloswoss then Anderson I also do not know much, I saw some examples like this, but I do not know exactly with q type the Thymeleaf sends these files, at first tried with Java File but also did not roll

  • @nullptr I followed the tip of the friend down there Tiago, he changed the stack, I’ll go back as was and post for you to see

Show 1 more comment

3 answers

0


I managed to solve the problem as follows:

at the frontend:

                    <form method="POST" action="/upload" enctype="multipart/form-data">
                    <input type="file" name="file" /><br />
                    <br /> <input type="submit" value="Submit" />
                </form>

on my controller:

    @PostMapping("/upload")
public void importar(@RequestParam("file") MultipartFile mf) {
    System.out.println("Iniciando a importação do arquivo: "+mf.getOriginalFilename());
    try {
        importService.montaAbastecimento(mf);
    } catch (Exception e) {
        System.out.println("Erro ao importar: " + e.getMessage());
    }

}

ai in the service only do normal reading of a Java file using Inputstreamreader and catching mf.getInputStream()

0

Use the Multiparthttpservletrequest

    public void upload(MultipartHttpServletRequest request) {
            MultipartFile multipartFile = request.getFile(request.getFileNames().next());
    }
  • Good afternoon James, thanks for the force, but now I had another mistake, it is necessary, change something in front-end too?

  • I’ve been researching here a little bit about the error but found nothing like it: java.lang.Illegalstateexception: Current request is not of type [org.springframework.web.multipart.Multiparthttpservletrequest]: org.springframework.web.servlet.Resource.Resourceurlencodingfilter$Resourceurlencodingrequestwrapper@3620a9de

  • By the way you are not using Spring together with Thymeleaf. This Multiparthttpservletrequest class is from the spring web mvc package.

0

nullptr, I’m back as I was, follow the stack

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "upload", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:865) ~[thymeleaf-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:608) ~[thymeleaf-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1087) [thymeleaf-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1061) [thymeleaf-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335) [thymeleaf-spring4-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:189) [thymeleaf-spring4-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) [spring-webmvc-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) [spring-webmvc-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) [spring-webmvc-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.springframework.web.servlet.resource.ResourceUrlEncodingFilter.doFilter(ResourceUrlEncodingFilter.java:63) [spring-webmvc-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) [spring-web-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) [spring-web-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) [tomcat-embed-core-8.5.40.jar:8.5.40]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.40.jar:8.5.40]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_201]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_201]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.40.jar:8.5.40]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]

[2m2019-06-24 17:36:28.025[0;39m [31mERROR[0;39m [35m5192[0;39m [2m---[0;39m [2m[nio-8080-exec-7][0;39m [36mo.a.c.c.C.[. [. [/]. [dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for Servlet [dispatcherServlet] in context with path with [] threw Exception [Request Processing failed; nested Exception is org.thymeleaf.exceptions.Templateinputexception: Error resolving template "upload", template Might not exist or Might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.Templateinputexception: Error resolving template "upload", template Might not exist or Might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(Templatemanager.java:865) ~[Thymeleaf-3.0.2.RELEASE. jar:3.0.2.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(Templatemanager.java:608) ~[Thymeleaf-3.0.2.RELEASE. jar:3.0.2.RELEASE] at org.thymeleaf.Templateengine.process(Templateengine.java:1087) ~[Thymeleaf-3.0.2.RELEASE. jar:3.0.2.RELEASE] at org.thymeleaf.Templateengine.process(Templateengine.java:1061) ~[Thymeleaf-3.0.2.RELEASE. jar:3.0.2.RELEASE] at org.thymeleaf.spring4.view.Thymeleafview.renderFragment(Thymeleafview.java:335) ~[Thymeleaf-spring4-3.0.2.RELEASE.jar:3.0.2.RELEASE] at org.thymeleaf.spring4.view.Thymeleafview.render(Thymeleafview.java:189) ~[Thymeleaf-spring4-3.0.2.RELEASE.jar:3.0.2.RELEASE] at org.springframework.web.servlet.Dispatcherservlet.render(Dispatcherservlet.java:1286) ~[spring-webmvc-4.3.24.RELEASE. jar:4.3.24.RELEASE] at org.springframework.web.servlet.Dispatcherservlet.processDispatchResult(Dispatcherservlet.java:1041) ~[spring-webmvc-4.3.24.RELEASE. jar:4.3.24.RELEASE] at org.springframework.web.servlet.Dispatcherservlet.doDispatch(Dispatcherservlet.java:984) ~[spring-webmvc-4.3.24.RELEASE. jar:4.3.24.RELEASE] at org.springframework.web.servlet.Dispatcherservlet.doService(Dispatcherservlet.java:901) ~[spring-webmvc-4.3.24.RELEASE. jar:4.3.24.RELEASE] at org.springframework.web.servlet.Frameworkservlet.processRequest(Frameworkservlet.java:970) ~[spring-webmvc-4.3.24.RELEASE. jar:4.3.24.RELEASE] at org.springframework.web.servlet.Frameworkservlet.doGet(Frameworkservlet.java:861) ~[spring-webmvc-4.3.24.RELEASE. jar:4.3.24.RELEASE] at javax.servlet.http.HttpServlet.service(Httpservlet.java:635)]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.