1

For my project, I am creating an expense management system. There is a place where the user can select an image file from local disk and add it as an attachment when adding a new expense.

The uploaded image should be displayed in a jLabel and then should be saved inside the project folder (say for e.g. /src/accounts/media) when clicking the save button, and the path should be saved inside a varchar column in mysql database for later retrieval.

For now, I can upload the image and display the image in jLabel.

Can anyone help me out on how save that image file inside a folder and to store the path in database?

    JFileChooser chooser = new JFileChooser();

    FileFilter ft = new FileNameExtensionFilter("Image Files", "jpg", "png", "jpeg");
    //FileFilter ft2 = new FileNameExtensionFilter("PDF Files", "pdf");

    chooser.addChoosableFileFilter(ft);
    //chooser.addChoosableFileFilter(ft2);
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    filePath = f.getAbsolutePath().toString();

    BufferedImage img = null;
    try {
        img = ImageIO.read(new File(filePath));
    } catch (IOException e) {
        e.printStackTrace();
    }

    Image dimg = img.getScaledInstance(lblAttachment.getWidth(), lblAttachment.getHeight(), Image.SCALE_SMOOTH);


    ImageIcon icon = new ImageIcon(dimg);
    lblAttachment.setText("");
    lblAttachment.setIcon(icon);
FireDrakon
  • 275
  • 2
  • 6
  • 19
  • possible duplicate of [Saving an image to .jpg file : Java](http://stackoverflow.com/questions/15095934/saving-an-image-to-jpg-file-java) – ControlAltDel Aug 29 '14 at 16:52

1 Answers1

0

To copy file to another location, you can choose any

Way1

Way2

To store the path, make a file-path column write insert query (SQL) :

prepareStatement  ps = conn.prepareStatement("INSERT INTO filePath VALUES(?, ?, ?)" );

         ps.setString(1,filePath);
         ps.set..
     //  conn, connection object

Full tutorial Here

Edit:

To save it in you some default Resources folder, You can obtain path, for EG:

public class ClassDemo {

  public static void main(String[] args) throws Exception {

    ClassDemo c = new ClassDemo();
    Class cls = c.getClass();

    // finds resource relative to the class location
    URL url = cls.getResource("file.txt");
    System.out.println("Value = " + url);

    // finds resource relative to the class location
    url = cls.getResource("newfolder/a.txt");
    System.out.println("Value = " + url);
  }
}
Community
  • 1
  • 1
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • Thanks! Can you explain me how I can save the image inside a project folder using relative path option? because the project location can be varied, and I need some portability options. I want to copy the image inside the folder /src/exp/resources – FireDrakon Aug 31 '14 at 06:49
  • Use thefull path to your folder. Lets say `C://Users//U_name//MyDocuments//NetBeansProject.....ProjectFolderName`.. – joey rohan Aug 31 '14 at 06:54
  • yh.. but what if the project location can be varied? If I can use a relative path like /src/resources it would be easier to save the image no matter where the project is located is. right? – FireDrakon Aug 31 '14 at 07:23
  • Thank you. :-) I tried the FileUtils and i worked like a charm. And for relative location, I used System.getProperty("user.dir") and concatenated the rest of the path to create the filepath. – FireDrakon Sep 01 '14 at 15:01