Is there any way to attach file to mysql in java? I need the file to be inside the database not the path.
Asked
Active
Viewed 1,363 times
0
-
Can you provide some more context? What have you tried so far, and what are you struggling with? – Nico Haase Jul 31 '18 at 07:22
2 Answers
0
This is example code of how one can write a binary file like a PDF document, a MS Excel spreadsheet, a JPG/PNG image file or a ZIP file, etc.. to a database table column of type BLOB, and read from the database.
I had used these with Java SE 7 or greater with the Apache Derby (a.k.a. Java DB) and MySQL databases respectively.
Derby: Write to db:
Path path = Paths.get("MyPic.jpg");
InputStream instream = Files.newInputStream(path);
PreparedStatement pstmnt = getConnection().prepareStatement(dml); // dml is an sql Insert
pstmnt.setBinaryStream(1, instream);
// pstmnt.setNull(1, Types.BLOB); // to set null value in db
pstmnt.executeUpdate();
pstmnt.close();
instream.close();
Read from db:
PreparedStatement pstmnt = getConnection().prepareStatement(sql); // sql is a Select
ResultSet rs = pstmnt.executeQuery();
rs.next();
InputStream instream = rs.getBinaryStream("col_name");
Path path = Paths.get("MyPic.jpg");
OutputStream outstream = Files.newOutputStream(path);
int len = 0;
byte [] buf = new byte [1024];
while ((len = instream.read(buf)) > 0) {
outstream.write(buf, 0, len);
}
instream.close();
outstream.flush();
outstream.close();
pstmnt.close();
MySQL:
Write to db:
PreparedStatement pstmnt_= conn.prepareStatement(DML) // sql Insert
InputStream instream = Files.newInputStream(filePath); // filePath is of type Path
pstmnt.setBinaryStream(1, instream);
pstmnt.executeUpdate();
// close resources here
Read from db:
PreparedStatement pstmnt = conn.prepareStatement(DML); // sql Select
ResultSet rs = pstmnt.executeQuery();
rs.next();
Blob blob = rs.getBlob("col_name");
long len = blob.length();
byte [] fileBytes = blob.getBytes(1L, (int) len); // start pos = 1L
OutputStream out = ...
out.write(fileBytes);
out.flush();
out.close();
Note that after using the JDBC objects (e.g., PreparedStatement
) and file io streams (e.g., InputStream
) make sure these resources are closed.

prasad_
- 12,755
- 2
- 24
- 36
-1
Use base64 enconding and save it as BLOB data
This is an example of encoding:
/**
* Method used for encode the file to base64 binary format
* @param file
* @return encoded file format
*/
private String encodeFileToBase64Binary(File file){
String encodedfile = null;
try {
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
encodedfile = Base64.encodeBase64(bytes).toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encodedfile;
}

Frighi
- 475
- 4
- 17
-
-
There is no need to use base64 encoding to store it in the database. Blobs are for binary data. The overhead of encoding using base64 is entirely unnecessary. – Mark Rotteveel Jul 31 '18 at 15:02