Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ru.bmstu;
- import java.nio.charset.StandardCharsets;
- import org.apache.commons.io.IOUtils;
- import sun.audio.*;
- import java.io.File;
- import java.awt.Image;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.lang.String;
- import java.net.ConnectException;
- import java.sql.Blob;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.naming.NamingException;
- import java.io.BufferedInputStream;
- import javazoom.jl.player.Player;
- import javax.sound.sampled.*; //AudioSystem;
- import java.io.InputStream;
- import java.io.IOException;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.DataLine;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.SourceDataLine;
- import java.io.FilterInputStream;
- import javax.ws.rs.core.Response;
- import java.sql.DriverManager;
- import org.apache.commons.io.IOUtils;
- import javax.servlet.http.*;
- import java.io.OutputStream;
- /**
- *
- * @author Dell
- */
- public class Filter {
- Connection conn;
- private long _blobId; //B
- private String MimeType;
- private String fileName;
- private boolean _controlErrors;
- private InputStream isFile;
- private String outFile;
- private InputStream filteredFile;
- public static void main(String[] args) {
- //String part1 = args[0];
- //String part2 = args[1];
- //int songID=Integer.parseInt(part1);
- //int methNum=Integer.parseInt(part2);
- int songID=150;
- Filter fil = new Filter(songID);
- String name=null;
- try{
- System.out.println("Устанавливаем соединение...");
- fil.initConnection();
- System.out.println("Выгружаем данные...");
- name=fil.initFile();
- System.out.println(name);
- System.out.println("Загружаем обработанные данные...");
- fil.toDataBase(name);
- System.out.println("Закрываем соединение...");
- fil.getConnection().close();
- }
- catch(Exception ex){
- ex.printStackTrace();
- }
- }
- Filter(int syncID){
- _blobId=syncID;
- MimeType="audio/aiff";
- }
- public String getMimeType(){
- return MimeType;
- }
- public InputStream getIsFile(){
- return isFile;
- }
- public InputStream getfilteredFile(){
- return filteredFile;
- }
- public Connection getConnection(){
- return conn;
- }
- public void initConnection() throws SQLException, NamingException, ClassNotFoundException {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection(
- "jdbc:oracle:thin:@localhost:1521:ORCL", "Mokona",
- "Mokona");
- conn.setAutoCommit(false);
- }
- public String initFile() throws SQLException {
- boolean wasQuery;
- try (PreparedStatement ps = conn.prepareStatement("select t.blob_data, t.file_name from audio t where t.sync_id = ?")) {
- ResultSet rs;
- Blob blob;
- wasQuery = false;
- while(!wasQuery && _blobId < 10000){
- ps.setLong(1, _blobId);
- rs = ps.executeQuery();
- System.out.println(ps.toString());
- while (rs.next()) {
- blob = rs.getBlob(1);
- isFile = blob.getBinaryStream();
- fileName = rs.getString(2);
- wasQuery = true;
- }
- rs.close();
- }
- }
- if (!wasQuery) {
- throw new RuntimeException("Строка с sync_id=" + Long.toString(_blobId) + " не найдена.");
- }
- return fileName;
- }
- public static void copyStream(InputStream input, OutputStream output)
- throws IOException
- {
- byte[] buffer = new byte[1024]; // Adjust if you want
- int bytesRead;
- while ((bytesRead = input.read(buffer)) != -1)
- {
- output.write(buffer, 0, bytesRead);
- }
- }
- public void toDataBase(String name) throws SQLException, IOException {
- try {
- String Query;
- int s=0;
- while (isFile.read()!=-1)
- s++;
- System.out.print("Размер файла:");
- System.out.println(s);
- Query = "declare ";
- Query+= "nSYNC_NEW number; ";
- Query+= "begin ";
- Query+= "nSYNC_NEW:=seq_id.nextval ; ";
- Query+= " insert into audio (sync_id, file_name, blob_data, mime_type) "
- + "values(nSYNC_NEW, ?, ?, 'audio/aiff'); ";
- Query+= " ? := nSYNC_NEW; ";
- Query+= " end;";
- CallableStatement cs = conn.prepareCall(Query);
- name="'shit_"+name+"'";
- cs.setString(1, name);
- cs.setBlob(2,isFile,s);
- cs.registerOutParameter(3, java.sql.Types.NUMERIC);
- cs.executeUpdate();
- long nSYNC_NEW = cs.getLong(3);
- System.out.print("ID обработанной композиции:");
- System.out.println(nSYNC_NEW);
- cs.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally {
- conn.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement