防止下载的文件互相替换或覆盖

时间:2016-06-27 14:19:11

标签: java mysql swing

如何使用Java应用程序下载MySQL数据库中的所有文件类型并阻止一个文件替换另一个文件?

如何在filepath=("D:\\sch work\\skirt\\filename.pdf");中包含所有文件类型,以便我能够使用JTable上的Java鼠标点击事件从MySQL数据库下载pdf,docx,jpeg等?

我有两个问题。

  1. 虽然我想打开docs,JPEG和img文件,但我只能打开PDF文件。
  2. 每次从其他行下载另一个文件时,它都会替换现有文件。我想保留所有这些
  3. 请在下面找到我的鼠标点击事件代码,请在我需要的时候建议我应该添加的内容。

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    
    
        String filePath =("D:\\sch work\\skirt\\filename.pdf");
          int BUFFER_SIZE = 4096;
    
          try {
        int cert_code= jTable1.getSelectedRow();
        String tableClick=(jTable1.getModel().getValueAt(cert_code,3).toString());
    
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee_certificate","root","");
             String sql= "SELECT cert FROM certificate WHERE cert_code =?" ;
                 PreparedStatement pstmt =  con.prepareStatement(sql);
    
      pstmt.setString(1, tableClick); 
      ResultSet rs=pstmt.executeQuery();
    if(rs.next()){
            Blob blob = rs.getBlob("cert");  
            InputStream inputStream = blob.getBinaryStream();
                OutputStream outputStream = new FileOutputStream(filePath);
    
                int bytesRead = -1;
                byte[] buffer = new byte[BUFFER_SIZE];
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
               inputStream.close();
                outputStream.close();
                 //JOptionPane.showMessageDialog(null,"file saved")
    } }
      catch (Exception e)
        {JOptionPane.showMessageDialog(null,e);}  
    }                                    
    

2 个答案:

答案 0 :(得分:1)

如果您需要PDF以外的其他内容,则应该存储该证书blob中的内容,以便发送相应的文件名/类型:

您获得PDF是因为它是您写出的确切+唯一文件类型:

String filePath =("D:\\sch work\\skirt\\filename.pdf");
                                                 ^^^

也许更像(伪代码):

select cert,filename,filetype from ...
filepath = 'd:\sch work\skirt' + filename
header('Content-type: ' + filetype);

答案 1 :(得分:0)

这是我的答案。上帝保佑Marc B。

private void jTable1MouseClicked(java.awt.event.MouseEvent evt){

      int BUFFER_SIZE = 4096;

      try {
    int cert_code= jTable1.getSelectedRow();
    String tableClick=(jTable1.getModel().getValueAt(cert_code,3).toString());

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee_certificate","root","");
         String sql= "SELECT cert, cert_name FROM certificate WHERE cert_code =?" ;
             PreparedStatement pstmt =  con.prepareStatement(sql);

  pstmt.setString(1, tableClick);
  ResultSet rs=pstmt.executeQuery();
if(rs.next()){
     String filename = rs.getString("cert_name");
    Blob blob = rs.getBlob("cert"); 
        InputStream inputStream = blob.getBinaryStream();   
            String filePath ="D:\\sch work\\skirt\\"+filename;
            OutputStream outputStream = new FileOutputStream(filePath);

            int bytesRead = -1;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
           inputStream.close();
            outputStream.close();
             //JOptionPane.showMessageDialog(null,"file saved")
} }
  catch (Exception e)
    {JOptionPane.showMessageDialog(null,e);}  
}