將其保存在MySQL數據庫我知道,我們可以使用BLOB數據類型,我已經如下使用它的代碼保存圖像在MySQL中,減小圖像的大小和與Java
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new JPEGImageFileFilter());
int res = fc.showOpenDialog(null);
try {
if (res == JFileChooser.APPROVE_OPTION) {
File image = new File(fc.getSelectedFile().getPath());
FileInputStream fis = new FileInputStream (image);
String sql="insert into imgtst (username,image) values (?, ?)";
pst=con.prepareStatement(sql);
pst.setString(1, user);
pst.setBinaryStream (2, fis, (int) file.length());
} else {
JOptionPane.showMessageDialog(null, "you must select image",
"Abortin", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception ioException) {
e.printStackTrace();
}
現在我需要什麼請確保保存到數據庫中的文件大小不應超過100 KB,如果超過該大小,我需要某種方法將圖像大小壓縮爲100 KB。請仔細閱讀您的寶貴建議。
你爲什麼不保存圖像文件夾中,並保存路徑2數據庫 – 2014-09-26 05:57:47
http://www.javalobby.org/articles/ultimate-image/ #11 – 2014-09-26 05:59:32
因爲圖像是以某種方式壓縮的,所以圖像最終的實際字節大小很難確定,因爲壓縮會在圖像中引入許多不同的變量... – MadProgrammer 2014-09-26 06:00:20