java - 将大图像上传到我在 java netbeans 中的应用程序

标签 java image swing netbeans

我正在创建一个应用程序。我想上传图像并保存到我的数据库..当运行我的应用程序时,它成功运行小尺寸图像,但我无法将大尺寸图像保存到我的数据库..当我要保存大尺寸图像(如 2 MB)时,它会提示类似的错误

com.mysql.jdbc.PacketTooBigException:查询数据包太大 (2491559 > 1048576)。您可以通过设置 max_allowed_pa​​cket' 变量在服务器上更改此值。

为什么会出现这个错误..我该如何解决它 这是我的代码

私有(private) void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

if (evt.getSource() == jButton1)
     {
        int x = 0;
        String s1 = jTextField1.getText().trim();
        String s2 = jTextField2.getText();

        char[] s3 = jPasswordField1.getPassword();
        char[] s4 = jPasswordField2.getPassword(); 
        String s8 = new String(s3);
        String s9 = new String(s4);

        String s5 = jTextField5.getText();
        String s6 = jTextField6.getText();
        String s7 = jTextField7.getText();

        if(s8.equals(s9))
        {
            try
            {
                File image1 = new File(filename);
        FileInputStream fis = new FileInputStream(image1);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte buf[] = new byte[1024];
        for (int readNum; (readNum = fis.read(buf)) != -1;) {

            bos.write(buf, 0, readNum);
        }
        cat_image = bos.toByteArray();
        System.out.println(cat_image.length);

            PreparedStatement ps = conn.prepareStatement("insert into reg values(?,?,?,?,?,?,?)");
                ps.setString(1, s1);
                ps.setString(2, s2);
                ps.setString(3, s8);
                ps.setString(4, s5);
                ps.setString(5, s6);
                ps.setString(6, s7);
                ps.setBytes(7, cat_image);
                int rs = ps.executeUpdate();
                x++;
                if (x > 0) 
                {
                    JOptionPane.showMessageDialog(jButton1, "Data Saved Successfully");
                }
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(jButton1, "Password Dosn't match");
        }

}
 else
{
    jTextField1.setText("");
        jTextField2.setText("");
        jPasswordField1.setText("");
        jPasswordField2.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
        jTextField7.setText("");
}



// TODO add your handling code here:
}                                        

私有(private) void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

jTextField1.setText("");
        jTextField2.setText("");
        jPasswordField1.setText("");
        jPasswordField2.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
        jTextField7.setText("");



// TODO add your handling code here:
}                                        

私有(private)无效jButton3ActionPerformed(java.awt.event.ActionEvent evt){

JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    filename = f.getAbsolutePath();
    jTextField3.setText(filename);


       // int returnVal = chooser.showOpenDialog(null);

    //if(returnVal == JFileChooser.APPROVE_OPTION) {

            ImageIcon icon=new ImageIcon(filename);
            jLabel8.setIcon(icon);

    //}




 // TODO add your handling code here:
 }

最佳答案

由于异常表明问题与服务器的 max_allowed_packet 有关。定义一个数据包的最大大小的变量。请参阅Packet Too Large描述如何修改服务器的配置文件以增加此变量的值的主题。另请参阅Using Option Files有关配置文件的更多详细信息。

关于java - 将大图像上传到我在 java netbeans 中的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22363833/

相关文章:

java - XML读取属性和元素

java - (Java)统计分析作业

css - 这个数据css是什么?

javascript - 将文本链接转换为图像链接

java - 我可以在新安装的 Linux 中使用 Java swing 应用程序吗

java - 设置 JFrame 的背景颜色

java - 将java文件写入C盘访问错误

java - 房间错误 : Not sure how to handle insert method's return type

javascript - 即使放在 $(window).load 内,我也会得到 $(image).width() == 0

java - 更新数据库后刷新JTable数据