When downloading a file with a web application ... The first thing that comes to mind is to set up a file server, but this method is also ... (I don't care about the size.)
main
//File reading
File uploadFile = new File("[directory]\\tmp.xlsx");
FileInputStream finstream = new FileInputStream(uploadFile);
Very simple.
main
Class.forName("oracle.jdbc.driver.OracleDriver");
//Connect to Oracle8i
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:TEST", "system", "password");
//Create statement
Statement stmt = conn.createStatement();
//SQL definition
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO [Schema].test_table(file_id, deta) VALUES(0, ?)");
//Bind to statement
pstmt.setBinaryStream(1, finstream, (int) uploadFile.length());
//Run
int result = pstmt.executeUpdate();
Binary with setBinaryStream and set.
main
//Binary data acquisition
ResultSet rset = stmt.executeQuery("select deta from [Schema].upload_file where file_id = '0'");
//Get Blob data and output to a file
Blob blob = null;
while (rset.next()) {
blob = rset.getBlob("deta");
}
byte[] buffer = blob.getBytes(1, (int) blob.length());
Store in byte array with getBytes
main
//Specify output file
File file = new File("[directory]\\test001_output.xlsx");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
//write byte array
dos.write(buffer);
dos.close();
Simply generate and write a file.
main
//Close result set
rset.close();
//Close statement
stmt.close();
//Close connection
conn.close();
//tmp file deletion
uploadFile.delete();
Various closes. And delete the uploaded temp file. A file is created in the specified directory.
Last time I generated Excel using POI, I want to binaryize the file generation process and keep it in the DB with the same flow ... (I spit it out as a temp file once, read it again and make it binary)
I can't think of it for a moment, so let's try it next time!
Recommended Posts