/**
* file读到byte[]
* @param file
* @return
* @throws IOException
* 2010-10-18 10:13:02
*/
private static byte[] getBytesFromFile(File file) throws IOException {
//file size
long length = file.length();
InputStream is = null;
is = new BufferedInputStream(new FileInputStream(file));
if (length > Integer.MAX_VALUE) {
throw new IOException("File is to large " + file.getName());
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file " + file.getName());
}
is.close();
return bytes;
}
DAO
public void save(final IETIcon icon) {
String insertsql = "insert into " + TABLENAME + "(" + COLUMN
+ ") values(?,?,?,?)";
super.getJdbcTemplate().execute(insertsql,
new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
ps.setString(1, icon.getAirlineCode());
ps.setBytes(2, icon.getB());//byte[]
ps.setString(3, icon.getRemark());
ps.setString(4, icon.getFileName());
ps.execute();
return null;
}
});
}