如何使用Java Spring MVC上传文件夹?

问题描述 投票:0回答:1

我正在尝试通过使用Spring MVC JSP上传包含csv文件的文件夹。

这是我的JSP文件:

<form:form id="regForm" modelAttribute="patient" action="../../AddPatient/${reqUserName}/add" method="post"
                           enctype="multipart/form-data"
                           class="w3-container w3-card-4 w3-light-grey w3-margin">


      <div class="w3-row w3-section">
           <div class="w3-col" style="width:200px">* Input Data Files</div>
           <div class="w3-rest">
                <input type="file" name="fileUpload" webkitdirectory directory />
            </div>
      </div>

      <%--button submit--%>
      <form:button id="addPatient" name="addPatient" class="w3-block w3-section w3-blue w3-padding">Submit</form:button>
</form:form>

这是我的控制器:

@RequestMapping(value = "/AddPatient/{reqUserName}/add", method = RequestMethod.POST)
    public ModelAndView saveAddPatient(@ModelAttribute("patient") Patient patient, BindingResult result,
                                       @PathVariable String reqUserName, SessionStatus status,
                                       HttpServletRequest request,
                                       @RequestParam CommonsMultipartFile[] fileUpload) {

        boolean error = false;


        if (fileUpload != null && fileUpload.length > 0) {
            for (CommonsMultipartFile aFile : fileUpload) {

                System.out.println("Saving file: " + aFile.getOriginalFilename());

                UploadFile uploadFile = new UploadFile();
                uploadFile.setFileName(aFile.getOriginalFilename());
                uploadFile.setData(aFile.getBytes());
                patientService.save(uploadFile);

            }

        }
        ModelAndView mav = new ModelAndView("success_Add_Patient", "code_module", patient.getCode_module());
        mav.addObject("reqUserName", reqUserName);
        return mav;

    }

这是我的PatientService.java:

void save(UploadFile uploadFile);

这是我的PatientServiceImpl.java:

@Override
    public void save(UploadFile uploadFile) {
        patientDao.save(uploadFile);
    }

这是user-beans.xml中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.Patient" />
    <context:annotation-config />

    <!--<bean id="patientService" class="com.patient.service.AssignmentServiceervice" />-->
    <bean id="patientService" class="com.Patient.service.PatientServiceImpl" />
    <bean id="patientDao" class="com.Patient.dao.PatientDaoImpl" >
        <constructor-arg ref="hibernate5AnnotatedSessionFactory"/>
    </bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <bean id="hibernate5AnnotatedSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>



    <!--set the jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="datasource" />
    </bean>

    <!--set the datasource bean-->
    <bean id="datasource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/db_team17" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <!--set the message source-->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="log4j" />
    </bean>

</beans>

这是我的数据库mySQL中的表“附件”:

table attachment

此表包含id作为主键,类型为bigint(20),以及file_data,类型为blob。

这是我尝试在构建的网络上上传文件夹后出现的错误:

org.hibernate.HibernateException: save is not valid without active transaction
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
    at com.sun.proxy.$Proxy31.save(Unknown Source)
    at com.Patient.dao.PatientDaoImpl.save(PatientDaoImpl.java:136)
    at com.Patient.service.PatientServiceImpl.save(PatientServiceImpl.java:68)
    at com.Patient.controller.PatientController.saveAddPatient(PatientController.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:870)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:776)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

任何人都可以帮助我如何解决此问题,以便我可以将文件夹内的那些文件成功上传到数据库mySQL中?有关其他信息,我只需要上传包含文件(CSV)而不是zip文件的“文件夹”。

谢谢

hibernate spring-mvc jsp servlets
1个回答
1
投票
在应用程序配置文件中删除hibernate.current_session_context_class或更改为<property name="current_session_context_class">jta</property>
© www.soinside.com 2019 - 2024. All rights reserved.