2014-03-12 49 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

相關問題