Jfinal 存byte[] 到mysql数据库中blob类型

 public void add(){
        
        UploadFile file = getFile("file");
        byte[] bytes =  {};
        if (file != null) {
            bytes = FileUtils.getBytes(file.getFile());
        }
        //许可证号
        String xkzh01 = this.getPara("XKZH01");
        String xkzh02 = this.getPara("XKZH02");
        String xkzh = "TS"+xkzh01+"-"+xkzh02;
        //检验单位ID
        String jyUnitID = ((SysUser)this.getSession().getAttribute("_user")).getUnitid();
        DictJysq jysq = getModel(DictJysq.class, "jysq");
        
        jysq.setXKZH(xkzh);
        jysq.setJYUnitID(jyUnitID);
        jysq.setSQRQ(new Date());
        
        //这个字段为blob类型
        jysq.setLicImage(bytes);
        jysq.save();
        
        renderNull();
    }

存入之前的bytes 数组的字节数为:879394

图片大小没有超过1m的

在执行sql语句后 报错:

2017-09-20 14:55:33
[ERROR]-[Thread: http-nio-8080-exec-3]-[com.jfinal.core.ActionHandler.handle()]: /dqjy/jyzzba/add
com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1759032 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
    at com.jfinal.plugin.activerecord.Model.save(Model.java:413)
    at dqjy.JyzzbaController.add(JyzzbaController.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.jfinal.aop.Invocation.invoke(Invocation.java:73)
    at Home.CheckUser.intercept(CheckUser.java:23)
    at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
    at com.jfinal.core.ActionHandler.handle(ActionHandler.java:74)
    at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:744)
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1759032 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3778)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2471)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:410)
    at com.jfinal.plugin.activerecord.Model.save(Model.java:408)
    ... 29 more


评论区

JFinal

2017-09-20 15:22

这个大小限制是 my.cnf 与 my.ini 的配置,不是字段,注意看异常中的:max_allowed_packet

先生-卫江涛

2017-09-20 15:28

@JFinal 这样解决不就是修改数据库的配置了吗?配置默认是1m,我上传的字节和图片都没 过 1m呢,

JFinal

2017-09-20 16:10

@先生-卫江涛 从异常提示来判断是改数据库配置文件

热门反馈

扫码入社