upload file (pdf,JPEG/FIF) to mysql using java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


upload file (pdf,JPEG/FIF) to mysql using java



Is there any way to attach file to mysql in java , I need the file to be inside the database not the path .





Possible duplicate of Storing Image in Data Base Using Java in Binary Format
– dieend
10 hours ago





Can you provide some more context? What have you tried so far, and what are you struggling with?
– Nico Haase
1 hour ago




2 Answers
2



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;
}





Base64 usually increase 20-30% file size.
– Leonardo G. Roese
4 hours ago



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.


PreparedStatement


InputStream






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived