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

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

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

问题

以下是翻译好的部分:

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

以下是 SQL 错误信息:

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

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

以下是下载按钮的 onAction 方法:

  1. FileDownloadButton.setOnAction(e -> {
  2. Item chosenItem = getTableView().getItems().get(getIndex());
  3. File file = new File(chosenItem.getPaperName());
  4. try {
  5. String filequery = "Select File from items Where" + "itemID" + " = " + chosenItem.getId();
  6. PreparedStatement pst = connection.prepareStatement(filequery);
  7. ResultSet rs = pst.executeQuery();
  8. FileOutputStream fileout = new FileOutputStream(file);
  9. while (rs.next()) {
  10. InputStream fileinput = result.getBinaryStream("file");
  11. byte[] buffer = new byte[1024];
  12. while (fileinput.read(buffer) > 0) {
  13. 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:

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

  1. FileDownloadButton.setOnAction(e -> {
  2. Item chosenItem = getTableView().getItems().get(getIndex());
  3. File file = new File(chosenItem.getPaperName());
  4. try {
  5. String filequery = "Select File from items Where" + "itemID" + " = " + chosenItem.getId();
  6. PreparedStatement pst = connection.prepareStatement(filequery);
  7. ResultSet rs = pst.executeQuery();
  8. FileOutputStream fileout = new FileOutputStream(file);
  9. while (rs.next()) {
  10. InputStream fileinput = result.getBinaryStream("file");
  11. byte[] buffer = new byte[1024];
  12. while (fileinput.read(buffer) > 0) {
  13. fileout.write(buffer);

答案1

得分: 0

以下是翻译好的内容:

这是对我有效的做法:

  1. FileDownloadButton.setOnAction(e -> {
  2. Item chosenItem = getTableView().getItems().get(getIndex());
  3. byte[] buffer = new byte[1024];
  4. String filename = null;
  5. try {
  6. String filequery = "Select File from items Where itemID = " + chosenItem.getId();
  7. PreparedStatement pst = connection.prepareStatement(filequery);
  8. ResultSet rs = pst.executeQuery();
  9. if (rs.next()) {
  10. try {
  11. InputStream fileIn = rs.getBinaryStream("File");
  12. buffer = rs.getBytes("File");
  13. filename = chosenItem.getPaperName();
  14. FileOutputStream fileout = new FileOutputStream("D:/Downloads/" + filename);
  15. while (fileIn.read(buffer) > 0) {
  16. fileout.write(buffer);
  17. }
  18. } catch (FileNotFoundException ex) {
  19. /*...*/
  20. }
  21. }
  22. /*finally open the file*/
  23. Desktop.getDesktop().open(new File("D:/Downloads/" + filename));
英文:

Here is what has worked for me

  1. FileDownloadButton.setOnAction(e -> {
  2. Item chosenItem = getTableView().getItems().get(getIndex());
  3. byte[] buffer = new byte[1024];
  4. String filename = null;
  5. try {
  6. String filequery = "Select File from items Where itemID = " + chosenItem.getId();
  7. PreparedStatement pst = connection.prepareStatement(filequery);
  8. ResultSet rs = pst.executeQuery();
  9. if (rs.next()) {
  10. try {
  11. InputStream fileIn = rs.getBinaryStream("File");
  12. buffer = rs.getBytes("File");
  13. filename = chosenItem.getPaperName();
  14. FileOutputStream fileout = new FileOutputStream("D:/Downloads/" + filename);
  15. while (fileIn.read(buffer) > 0) {
  16. fileout.write(buffer);
  17. }
  18. } catch (FileNotFoundException ex) {
  19. /*...*/
  20. }
  21. }
  22. /*finally open the file*/
  23. 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:

确定