【问题标题】:Upload large image to my application in java netbeans在 java netbeans 中将大图像上传到我的应用程序
【发布时间】:2014-04-17 07:32:36
【问题描述】:

我正在创建一个应用程序。我想上传图像并保存到我的数据库..运行我的应用程序时,它可以成功运行小尺寸图像,但我无法将大尺寸图像保存到我的数据库..当我要保存像 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:
 }

【问题讨论】:

  • com.mysql.jdbc.PacketTooBigException: Packet for query is too large (2491559 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable. 这似乎是服务器端的问题,所以您可能应该在那里而不是在您的代码中查看解决方案。

标签: java image swing netbeans


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多