将MultipartFile作为Blob发送到mysql数据库

时间:2013-10-01 21:32:05

标签: java mysql nullpointerexception blob ibatis

所以我有一个注册表单能够使用ibatis发送一个配置文件图片存储在我的数据库中,但是当我尝试插入时,我得到一个nullpointerexception

栈跟踪

SEVERE: Servlet.service() for servlet [volts] in context with path [/Mugenjou] threw exception [Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];   
--- The error occurred in com/mugenjou/integration/sql.xml.  
--- The error occurred while applying a parameter map.  
--- Check the sql.register-InlineParameterMap.  
--- Check the parameter mapping for the 'picture' property.  
--- Cause: java.lang.NullPointerException; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in com/mugenjou/integration/sql.xml.  
--- The error occurred while applying a parameter map.  
--- Check the sql.register-InlineParameterMap.  
--- Check the parameter mapping for the 'picture' property.  
--- Cause: java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.ibatis.sqlmap.engine.type.UnknownTypeHandler.setParameter(UnknownTypeHandler.java:69)
    at com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap.setParameter(ParameterMap.java:166)
    at com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap.setParameters(ParameterMap.java:126)
    at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:78)
    at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216)
    at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94)
    at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:393)
    at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
    at org.springframework.orm.ibatis.SqlMapClientTemplate$8.doInSqlMapClient(SqlMapClientTemplate.java:366)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:200)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.insert(SqlMapClientTemplate.java:364)
    at com.mugenjou.integration.BusinessServiceImpl.registerUser(BusinessServiceImpl.java:76)
    at com.mugenjou.control.controller.register(controller.java:101)
    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.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1813)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

一旦我得到文件,我尝试将其转换为可以使用这些方法读取的表单,两者都产生与stacktrace中显示的相同的结果。一个转换为SerialBlob,另一个转换为ByteArrayInputStream。

private ByteArrayInputStream convertMultipartFile(MultipartFile file) throws IOException, SQLException {

    byte[] bytes = file.getBytes();
    return new ByteArrayInputStream(bytes);
    //return new SerialBlob(bytes);

}

SQL.XML:

<insert id ="register" parameterClass ="java.util.Map">
    INSERT INTO ACCOUNT_INFO (FNAME, LNAME, ACCOUNT_NAME, PASSWORD, STATUS, CREATION_DATE, LAST_ACCESSED, PROFILE_PICTURE)
    VALUES (#fname#, #lname#, #account_name#, #pwd#, 0, #creation_date#, #last_accessed#, #picture#)

</insert>

BusinessServiceImpl

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void registerUser(ByteArrayInputStream picture, String fname, String lname, String account_name,
        String pwd) {

    sqlMap = getSqlMapClientTemplate();
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();

    Map params = new HashMap();

    params.put("fname", fname);
    params.put("lname", lname);
    params.put("account_name", account_name);
    params.put("pwd", pwd);
    params.put("creation_date", dateFormat.format(date));
    params.put("last_accessed", "0000-00-00 00:00:00");
    params.put("picture", picture);

    sqlMap.insert("sql.register", params);
}

1 个答案:

答案 0 :(得分:3)

通过将MultipartFile转换为InputStream然后存储

来解决此问题