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

时间:2014-03-12 21:06:50

标签: 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 void 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:
 }

1 个答案:

答案 0 :(得分:1)

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