Uploading/Downloading word 文件 to/from Mysql

Uploading/Downloading word file to/from Mysql

我正在创建一个应用程序,允许用户填写发件人并将他们的简历上传给潜在雇主。目前我无法将简历上传到我的数据库。

        //File chooser
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new java.io.File("C:\Users\Dawid\Desktop"));
    fc.setDialogTitle("CV Upload");
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    /*********************************************/
    //Buttons
    JButton open = new JButton();
    JButton btnBrowse = new JButton("Browse");
    btnBrowse.setBounds(312, 172, 89, 23);
    qualifications.add(btnBrowse);
    btnBrowse.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
            if(fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION);
            CvUploadTextField.setText(fc.getSelectedFile().getAbsolutePath());
        }
    });

这段代码设置了我的文件选择器并在文本框中打印出文件路径。我希望实现的是能够从该文本框获取文件路径并能够将该文件上传到数据库,方式与下面类似。

                 try 
              {
                  Class.forName(driver).newInstance();
                  Connection conn = DriverManager.getConnection(url+dbName,userName,password);
                  Statement st = conn.createStatement();
                  ResultSet res = st.executeQuery("SELECT * FROM javaapp.test");
                  int val = st.executeUpdate("INSERT INTO javaapp.test (Name,DOB,Phone,Email,Q1Name,Q1Title,Q2Name,Q2Title,W1Name,W1From,W1To,W2Name,W2From,W2To,About)"
                        + " VALUES ('"+Name+"','"+DOB+"','"+Phone+"','"+Email+"','"+Q1Name+"','"+Q1Title+"','"+Q2Name+"','"+Q2Title+"','"+W1Name+"','"+W1From+"',"
                                + "'"+W1To+"','"+W2Name+"','"+W2From+"','"+W2To+"','"+About+"')");
                  if(val==1)
                      System.out.print("Successfully inserted value");
                  conn.close();
              } 
              catch (Exception e) 
              {
                  e.printStackTrace();
              }

我知道文件不像字符串和整数,所以我不知道该怎么做。任何帮助将不胜感激。

谢谢

JFileChooser 有一个方法,getSelectedFile()。这是一个文件。你可以

如果您只想允许设置 .doc、.docx 扩展名

 JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "word docs only", "doc", "docx");
    chooser.setFileFilter(filter);

另存为

 File selectedFile = chooser.getSelectedFile();

        try {
            String fileName = selectedFile.getCanonicalPath();
            if (!fileName.endsWith(EXTENSION)) {
                selectedFile = new File(fileName + EXTENSION);
            }
            ImageIO.write(image, FORMAT_NAME, selectedFile);
        } catch (IOException e) {
            e.printStackTrace();

如果您想将 word 文件上传到数据库,您可能需要查找 BLOB 数据类型 mysql 以及 字节数组 对于 java。

建议: 最好将文件存储在您的服务器上并将这些文件的目录路径存储在您的数据库中,以便您知道在检索它们时在哪里查找。这不仅是一个简单的实现,而且也是明智的。