【发布时间】:2015-08-20 11:20:12
【问题描述】:
是否可以使用 Netbeans (Java) 将图像添加到数据库中?
当我们点击选择文件按钮时,我想先浏览图像,然后当你点击“添加”按钮时,它必须添加到数据库中。反正有这样做吗?
【问题讨论】:
标签: java database image netbeans upload
是否可以使用 Netbeans (Java) 将图像添加到数据库中?
当我们点击选择文件按钮时,我想先浏览图像,然后当你点击“添加”按钮时,它必须添加到数据库中。反正有这样做吗?
【问题讨论】:
标签: java database image netbeans upload
要将图像存储在数据库中,CLOB 或 BLOB 是数据库中保存图像的列的类型。 Netbeans 和 IDE 你要么需要 jsp(网页)或 java swing(JFileChooser),它会接受图像,然后你需要编写 java 代码将图像保存在数据库中。一些链接供参考。
在 JSP 中浏览和选择要上传的文件,您需要表单中的 HTML 字段。如 HTML 规范中所述,您必须使用 POST 方法,并且表单的 enctype 属性必须设置为“multipart/form-data”。
<form action="upload" method="post" enctype="multipart/form-data">
<input type="imageName" name="imageName" />
<input type="image" name="image" />
<input type="submit" />
</form>
现在这个输入相同,即 DB 中的 InputStream
PreparedStatement#setBinaryStream().
preparedStatement = connection.prepareStatement("INSERT INTO tablename (imageName, image) VALUES ( ?, ?)");
preparedStatement.setString(1, imageName);
preparedStatement.setBinaryStream(2, image);
如果可能,请使用http://commons.apache.org/proper/commons-fileupload/
http://commons.apache.org/proper/commons-fileupload/using.html
编辑:根据 cmets 添加 Swings 代码
编辑:
如前所述,使用了 Swings,因此发布代码以提供帮助。
JFileChooser 有一个方法,getSelectedFile()。这是一个文件。你可以
如果您只想允许设置 .doc、.docx 扩展名
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"word docs only", "doc", "docx");
chooser.setFileFilter(filter);
The save it as
File selectedFile = chooser.getSelectedFile();
try {
String fileName = selectedFile.getCanonicalPath();
if (!fileName.endsWith(EXTENSION)) {
selectedFile = new File(fileName + EXTENSION);
}
ImageIO.write(image, FORMAT_NAME, selectedFile);
} catch (IOException e) {
e.printStackTrace();
【讨论】: