Zero-dev
Zero-dev

Reputation: 340

Make name to PDF with JFileChooser

i use this code to save a pdf file on desktop from Mysql, but the file saved without extension, how can i save it automatcly with extension pdf, ?!!

JFileChooser JFileChooser = new JFileChooser(".");
        Activiter ac = new Activiter();

        int status = JFileChooser.showDialog(null,"Saisir l'emplacement et le nom du fichier cible");

              if(status == JFileChooser.APPROVE_OPTION)
              {
                try
                {
                  ac.chargeIMG(jTable3.getValueAt(rec, 6).toString(),JFileChooser.getSelectedFile().getAbsolutePath());
                }
                catch(Exception ex)
                {
                  JOptionPane.showMessageDialog(null,"Une erreur s'est produite dans le chargement de documents.");
                  ex.printStackTrace();
                }
              }

thanks for Help, methode chargerIMG that colled by ac.chargeIMG

chargeIMG is give the pdf file from MySQL, the code is

public void chargeIMG(String idpro, String location) throws Exception
    {
        // cnx 

      File monImage = new File(location);
      FileOutputStream ostreamImage = new FileOutputStream(monImage);

      try {

        PreparedStatement ps = conn.prepareStatement("SELECT img FROM projet WHERE idpro=?");

        try
        {
          ps.setString(1,idpro);
          ResultSet rs = ps.executeQuery();

          try
          {
            if(rs.next())
            {
              InputStream istreamImage = rs.getBinaryStream("img");

              byte[] buffer = new byte[1024];
              int length = 0;

              while((length = istreamImage.read(buffer)) != -1)
              {
                ostreamImage.write(buffer, 0, length);
          }
        }
          }
          finally
          {
            rs.close();
          }
        }
        finally
        {
          ps.close();
        }
      }
      finally
      {
        ostreamImage.close();
      }
    }

Upvotes: 0

Views: 1283

Answers (2)

Eng.Fouad
Eng.Fouad

Reputation: 117665

Override getSelectedFile():

import java.io.File;
import javax.swing.JFileChooser;

public class MyFileChooser extends JFileChooser
{
    private static final long serialVersionUID = 1L;
    private String extension;

    public MyFileChooser(String currentDirectoryPath, String extension)
    {
        super(currentDirectoryPath);
        this.extension = extension;
    }

    @Override
    public File getSelectedFile()
    {
        File selectedFile = super.getSelectedFile();
        if(selectedFile != null && (getDialogType() == SAVE_DIALOG || getDialogType() == CUSTOM_DIALOG))
        {
            String name = selectedFile.getName();
            if(!name.contains(".")) selectedFile = new File(selectedFile.getParentFile(), name + "." + extension);
        }
        return selectedFile;
    }
}

and then use it like:

JFileChooser chooser = new MyFileChooser(".", "pdf");

Upvotes: 2

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21778

The simplest solution, obtain the path (File.getPath), check if it ends by the expected extension and if not then replace it by another File with the extension present: new File(path+".pdf");

Upvotes: 1

Related Questions