Java 将图像保存在特定文件夹中,并在mysql中存储路径以供以后显示

Java 将图像保存在特定文件夹中,并在mysql中存储路径以供以后显示,java,mysql,swing,image-processing,netbeans,Java,Mysql,Swing,Image Processing,Netbeans,对于我的项目,我正在创建一个费用管理系统。在这里,用户可以从本地磁盘选择一个映像文件,并在添加新费用时将其作为附件添加 上传的图像应显示在jLabel中,然后在单击“保存”按钮时保存在项目文件夹中,例如/src/accounts/media,并且路径应保存在mysql数据库中的varchar列中,以便以后检索 现在,我可以上传图像并在jLabel中显示图像 有人能帮我把图像文件保存在文件夹中,并把路径存储在数据库中吗 JFileChooser chooser = new JFileCho

对于我的项目,我正在创建一个费用管理系统。在这里,用户可以从本地磁盘选择一个映像文件,并在添加新费用时将其作为附件添加

上传的图像应显示在jLabel中,然后在单击“保存”按钮时保存在项目文件夹中,例如/src/accounts/media,并且路径应保存在mysql数据库中的varchar列中,以便以后检索

现在,我可以上传图像并在jLabel中显示图像

有人能帮我把图像文件保存在文件夹中,并把路径存储在数据库中吗

    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);

要将文件复制到另一个位置,可以选择任何

要存储路径,请使用文件路径列写入插入查询SQL:

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

         ps.setString(1,filePath);
         ps.set..
     //  conn, connection object
完整教程

编辑:

要将其保存在某个默认资源文件夹中,您可以获取路径,例如:

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);
  }
}

可能重复的谢谢!您能解释一下如何使用相对路径选项将图像保存在项目文件夹中吗?因为项目位置可以变化,我需要一些可移植性选项。我想将映像复制到/src/exp/resources文件夹中,使用文件夹的完整路径。假设C://Users//U\U name//MyDocuments//NetBeansProject…..ProjectFolderName..yh。。但是,如果项目的位置可以改变呢?如果我可以使用像/src/resources这样的相对路径,那么无论项目位于何处,保存映像都会更容易。对吗?谢谢你我试过了菲律提尔,我工作得很有魅力。对于相对位置,我使用System.getPropertyuser.dir并连接路径的其余部分来创建文件路径。