[英]Access Storage.DIRECTORY_DOWNLOADS (Root/Download Folder To View a file)
[英]Custom cell button action to Download a file to the 'downloads' folder
所以我设法构建了我的自定义单元格,其中包含一个从本地数据库下载文件的按钮。 不知何故,一切似乎都很好,但是当我运行并尝试下载时,我收到一个 sql 错误,说明我的 sql 查询有问题。 我还希望用户选择保存下载文件的位置。 任何帮助都受到高度赞赏。
这是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
这是下载按钮的 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);
这是对我有用的
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.