2014-03-12 22 views
0

我正在创建一个应用程序。我想上传一个图像并保存到我的数据库..当运行我的应用程序它成功地运行小尺寸图像,但我不能保存大尺寸的图像到我的数据库..当我要保存像2 MB的大尺寸图像,它会提示出现如下错误:将大图片上传到我的Java应用程序netbeans

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

为什么就这样产生了error..how我可以解决它 这我的代码

私人无效jButton1ActionPerformed(EVT java.awt.event.ActionEvent中){

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: 
}           

私人无效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: 
}           

私人无效为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

'com.mysql.jdbc.PacketTooBigException:用于查询的数据包太大(2491559> 1048576)。您可以通过设置max_allowed_pa​​cket'变量来更改服务器上的此值。“这似乎是服务器端的问题,因此您可能应该在那里寻找解决方案,而不是在代码中。 – dic19

回答

1

由于异常状态的问题,是关系到服务器的max_allowed_packet变量,它定义一个数据包的最大尺寸。请参阅Packet Too Large主题,该主题介绍如何修改服务器的配置文件以增加此变量的值。有关配置文件的更多详细信息,另见Using Option Files

相关问题