如何在netbeans 7.0.1中上传图像

时间:2013-12-04 11:14:45

标签: netbeans-7

我对在NetBeans 7.0.1中上传图像存有疑问。 NetBeans 7.0.1中没有fileupload按钮。我们可以将它放入托盘或是否有其他方式上传图像?

1 个答案:

答案 0 :(得分:0)

我得到了我怀疑的答案,即我只是拿一个标签并设置一个图标,从文件对话框中读取路径并将图像转换为字节并插入到sql中,查询就是它 代码:

  try{
            fd1=new FileDialog(this,"Open",FileDialog.LOAD);          // filedialog box        open
        fd1.show();
        fos1=new FileInputStream(fd1.getDirectory()+fd1.getFile());   //get the path from filedialog box
        String fileName=fd1.getDirectory()+fd1.getFile();   
         java.io.File f = new java.io.File (fileName);              //read the file
        int fileSize = (int) f.length();                     // read the file size
          bytes= new byte[fileSize];                    //convert into bytes
         FileInputStream fis=new FileInputStream(f);
         fis.read(bytes);                              //read the bytes
        System.out.println("image inserted");
        ImageIcon icon = new ImageIcon(bytes);         // set bytes to icon
         photo.setIcon(icon);                          //set icon to label
         Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/schoolmngt","root","root");
       PreparedStatement pst=con.prepareStatement("update studentpersonal set image=? where stadmno='"+admno.getText()+"'");  
       pst.setBytes(1,bytes);                           //insert bytes into sql db
         pst.execute();
        }
        catch(Exception ee)
        {ee.printStackTrace();}