/** * Determine the type of attachment, based on file extension, and add it to the elog entry with * the entry_id. * * @param fname input filename, either an image or a text file * @param fileType "I" for image file, "A" for text file * @param entry_id ID of entry to which to add this file * @param caption Caption or 'title' for the attachment * @throws Exception */ private void addFileToElog( final String fname, String fileType, final int entry_id, final String caption) throws Exception { // Get the file extension final int ndx = fname.lastIndexOf("."); final String extension = fname.substring(ndx + 1); long fileTypeID = getFileTypeId(fileType, extension); // If the image type cannot be found in the RDB, change its file type to // an attachment and look for the // extension as an attachment if (fileTypeID == -1 && fileType.equals("I")) { fileType = "A"; fileTypeID = getFileTypeId(fileType, extension); } // Initiate the sql to add attachments to the elog final String mysql = "call logbook.logbook_pkg.add_entry_attachment" + "(?, ?, ?, ?, ?)"; final Connection connection = rdb.getConnection(); final CallableStatement statement = connection.prepareCall(mysql); try { statement.setInt(1, entry_id); statement.setString(2, fileType); statement.setString(3, caption); statement.setLong(4, fileTypeID); final File inputFile = new File(fname); // Send the image to the sql. if (fileType.equals("I")) { try { final int file_size = (int) inputFile.length(); final FileInputStream input_stream = new FileInputStream(inputFile); statement.setBinaryStream(5, input_stream, file_size); input_stream.close(); } catch (FileNotFoundException e1) { System.out.println("Could not find " + fname); return; } } // Send the text attachment to the sql else { // Create a Blob to store the attachment in. final BLOB blob = BLOB.createTemporary(connection, true, BLOB.DURATION_SESSION); blob.setBytes(1L, getBytesFromFile(inputFile)); statement.setBlob(5, blob); } statement.executeQuery(); } finally { statement.close(); } }
public static void sendToOracle(BeanAccesSQL DBSQL, LinkedList<Posters> posters, int idFilm) throws Exception { ArrayDescriptor ListString = ArrayDescriptor.createDescriptor("LISTVARCHAR2", DBSQL.getConnection()); ArrayDescriptor ListInt = ArrayDescriptor.createDescriptor("LISTNUMBER", DBSQL.getConnection()); ArrayDescriptor ListBlob = ArrayDescriptor.createDescriptor("LISTBLOB", DBSQL.getConnection()); String requete = "{ call PACKAGE_CB.EncoderPosters(?,?,?,?,?,?)}"; CallableStatement csmt = null, smt2 = null; byte[] imageInByte = null; Statement smt = null; int count = 0, nbCover = 0; InputStream isImage = null; LinkedList<Posters> listCover = new LinkedList<>(); for (Posters p : posters) { if (p.getImage().getSize().equals("cover")) { count++; listCover.add(p); if (count == 4) break; } } String[] id = new String[count]; String[] url = new String[count]; String[] size = new String[count]; int[] height = new int[count]; int[] width = new int[count]; BLOB[] img = new BLOB[count]; int i = 0; for (Posters s : listCover) { id[i] = s.getImage().getId(); height[i] = s.getImage().getHeight(); width[i] = s.getImage().getWidth(); url[i] = s.getImage().getUrl(); size[i] = s.getImage().getSize(); i++; } nbCover = i; for (i = 0; i < nbCover; i++) { try { URL urlImage = new URL(url[i]); BufferedImage image = ImageIO.read(urlImage); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", baos); baos.flush(); imageInByte = baos.toByteArray(); baos.close(); img[i] = BLOB.createTemporary(DBSQL.getConnection(), false, BLOB.DURATION_SESSION); OutputStream outputStream = img[i].setBinaryStream(0L); ByteArrayInputStream inputStream = new ByteArrayInputStream(imageInByte); byte[] buffer = new byte[img[i].getBufferSize()]; int bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); } catch (IOException ex) { Logger.getLogger(Posters.class.getName()).log(Level.SEVERE, null, ex); } } Array arrayId = new ARRAY(ListString, DBSQL.getConnection(), id); Array arraySize = new ARRAY(ListString, DBSQL.getConnection(), size); Array arrayHeight = new ARRAY(ListInt, DBSQL.getConnection(), height); Array arrayWidth = new ARRAY(ListInt, DBSQL.getConnection(), width); Array arrayImg = new ARRAY(ListBlob, DBSQL.getConnection(), img); try { csmt = DBSQL.getConnection().prepareCall(requete); csmt.setArray(1, arrayId); csmt.setArray(2, arraySize); csmt.setArray(3, arrayHeight); csmt.setArray(4, arrayWidth); csmt.setArray(5, arrayImg); csmt.setInt(6, idFilm); csmt.executeUpdate(); csmt.close(); smt = DBSQL.getConnection().createStatement(); smt.executeQuery("commit"); smt.close(); } catch (SQLException ex) { throw new Exception("Poster Exception: " + ex.getMessage()); } }