自定义单元格按钮操作,将文件下载到“downloads”文件夹中。

huangapple go评论80阅读模式
英文:

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

问题

以下是翻译好的部分:

我已经成功构建了一个自定义单元格,其中包含一个按钮,用于从我的本地数据库中下载文件。不知何故,一切似乎都很正常,但当我运行并尝试下载时,出现了一个 SQL 错误,表示我的 SQL 查询存在问题。我还希望用户能够选择保存下载文件的位置。非常感谢任何帮助。

以下是 SQL 错误信息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 在你的 SQL 语法中有一个错误;请检查与你的 MySQL 服务器版本对应的手册,了解在第 1 行附近使用的正确语法。

自定义单元格按钮操作,将文件下载到“downloads”文件夹中。

以下是下载按钮的 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);
英文:

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. 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:

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

自定义单元格按钮操作,将文件下载到“downloads”文件夹中。

here is the onaction method for the download button

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

答案1

得分: 0

以下是翻译好的内容:

这是对我有效的做法:

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));
英文:

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

huangapple
  • 本文由 发表于 2020年10月6日 17:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64222769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定