简体   繁体   English

将文件下载到“下载”文件夹的自定义单元格按钮操作

[英]Custom cell button action to Download a file to the 'downloads' folder

so I have managed to build my custom cell which holds a button to download a file from my local database.所以我设法构建了我的自定义单元格,其中包含一个从本地数据库下载文件的按钮。 somehow all seems fine but when i run and try to download, I get an sql error that there's a problem with my sql query.不知何故,一切似乎都很好,但是当我运行并尝试下载时,我收到一个 sql 错误,说明我的 sql 查询有问题。 I would also like the user to chose the location to save the downloaded file.我还希望用户选择保存下载文件的位置。 Any help is highly appreciated.任何帮助都受到高度赞赏。

here is the sql error:这是sql错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='20'' at line 1

在此处输入图片说明

here is the onaction method for the download button这是下载按钮的 onaction 方法

FileDownloadButton.setOnAction(e -> {

                                    Item chosenItem = getTableView().getItems().get(getIndex());
                                    File file = new File(chosenItem.getPaperName());
                                    try {
                                        String filequery = "Select File from items Where" + "itemID" + " = " + chosenItem.getId();
                                        PreparedStatement pst = connection.prepareStatement(filequery);
                                        ResultSet rs = pst.executeQuery();

                                        FileOutputStream fileout = new FileOutputStream(file);
                                        while (rs.next()) {
                                            InputStream fileinput = result.getBinaryStream("file");
                                            byte[] buffer = new byte[1024];
                                            while (fileinput.read(buffer) > 0) {
                                                fileout.write(buffer);

Here is what has worked for me这是对我有用的

FileDownloadButton.setOnAction(e -> {
                                    Item chosenItem = getTableView().getItems().get(getIndex());
                                    byte[] buffer = new byte[1024];
                                    String filename = null;

                                    try {

                                        String filequery = "Select File from items Where itemID = " + chosenItem.getId();
                                        PreparedStatement pst = connection.prepareStatement(filequery);

                                        ResultSet rs = pst.executeQuery();

                                        if (rs.next()) {
                                            try {
                                                InputStream fileIn = rs.getBinaryStream("File");

                                                buffer = rs.getBytes("File");
                                                filename = chosenItem.getPaperName();
                                                FileOutputStream fileout = new FileOutputStream("D:/Downloads/" + filename);
                                                while (fileIn.read(buffer) > 0) {
                                                    fileout.write(buffer);
                                                }
                                            } catch (FileNotFoundException ex) {
                                                /*...*/
                                            }

                                        }
                                        /*finally open the file*/
                                        Desktop.getDesktop().open(new File("D:/Downloads/" + filename));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM