Advertisement
Guest User

Untitled

a guest
May 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ru.bmstu;
  7. import java.nio.charset.StandardCharsets;
  8. import org.apache.commons.io.IOUtils;
  9. import sun.audio.*;
  10. import java.io.File;
  11. import java.awt.Image;
  12. import java.io.ByteArrayInputStream;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.lang.String;
  17. import java.net.ConnectException;
  18. import java.sql.Blob;
  19. import java.sql.CallableStatement;
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import javax.jws.WebMethod;
  26. import javax.jws.WebParam;
  27. import javax.naming.NamingException;
  28. import java.io.BufferedInputStream;
  29. import javazoom.jl.player.Player;
  30. import javax.sound.sampled.*; //AudioSystem;
  31. import java.io.InputStream;
  32. import java.io.IOException;
  33. import javax.sound.sampled.AudioFormat;
  34. import javax.sound.sampled.AudioInputStream;
  35. import javax.sound.sampled.AudioSystem;
  36. import javax.sound.sampled.DataLine;
  37. import javax.sound.sampled.LineUnavailableException;
  38. import javax.sound.sampled.SourceDataLine;
  39. import java.io.FilterInputStream;
  40. import javax.ws.rs.core.Response;
  41. import java.sql.DriverManager;
  42. import org.apache.commons.io.IOUtils;
  43. import javax.servlet.http.*;
  44. import java.io.OutputStream;
  45. /**
  46.  *
  47.  * @author Dell
  48.  */
  49. public class Filter {
  50.     Connection conn;
  51.     private long _blobId; //B
  52.     private String MimeType;
  53.     private String fileName;
  54.     private boolean _controlErrors;
  55.     private InputStream isFile;
  56.     private String outFile;
  57.     private InputStream filteredFile;
  58.    
  59.    
  60.    public static void main(String[] args) {
  61.     //String part1 = args[0];
  62.     //String part2 = args[1];
  63.    
  64.     //int songID=Integer.parseInt(part1);
  65.     //int methNum=Integer.parseInt(part2);
  66.     int songID=150;
  67.     Filter fil = new Filter(songID);
  68.     String name=null;
  69.     try{
  70.         System.out.println("Устанавливаем соединение...");
  71.         fil.initConnection();
  72.         System.out.println("Выгружаем данные...");
  73.         name=fil.initFile();
  74.         System.out.println(name);
  75.         System.out.println("Загружаем обработанные данные...");
  76.         fil.toDataBase(name);
  77.         System.out.println("Закрываем соединение...");
  78.         fil.getConnection().close();    
  79.         }
  80.         catch(Exception ex){
  81.             ex.printStackTrace();
  82.         }
  83.     }
  84.    
  85.     Filter(int syncID){
  86.        _blobId=syncID;
  87.        MimeType="audio/aiff";
  88.     }
  89.    
  90.     public String getMimeType(){
  91.         return MimeType;
  92.     }
  93.    
  94.     public InputStream getIsFile(){
  95.         return isFile;
  96.     }
  97.     public InputStream getfilteredFile(){
  98.        
  99.         return filteredFile;
  100.     }
  101.     public Connection getConnection(){
  102.         return conn;
  103.     }
  104.    
  105.     public void initConnection() throws SQLException, NamingException, ClassNotFoundException {
  106.         Class.forName("oracle.jdbc.driver.OracleDriver");
  107.         conn = DriverManager.getConnection(
  108.                 "jdbc:oracle:thin:@localhost:1521:ORCL", "Mokona",
  109.                 "Mokona");
  110.         conn.setAutoCommit(false);
  111.  
  112.     }
  113.     public String initFile() throws SQLException {
  114.         boolean wasQuery;
  115.         try (PreparedStatement ps = conn.prepareStatement("select t.blob_data, t.file_name from audio t where t.sync_id = ?")) {
  116.             ResultSet rs;
  117.             Blob blob;
  118.             wasQuery = false;
  119.            
  120.             while(!wasQuery && _blobId < 10000){
  121.                 ps.setLong(1, _blobId);
  122.                 rs = ps.executeQuery();
  123.                 System.out.println(ps.toString());
  124.                 while (rs.next()) {
  125.                    
  126.                     blob = rs.getBlob(1);
  127.                     isFile = blob.getBinaryStream();
  128.                     fileName = rs.getString(2);
  129.                     wasQuery = true;
  130.                 }
  131.                 rs.close();
  132.             }
  133.         }
  134.         if (!wasQuery) {
  135.             throw new RuntimeException("Строка с sync_id=" + Long.toString(_blobId) + " не найдена.");
  136.         }
  137.         return fileName;
  138.     }
  139.    
  140.     public static void copyStream(InputStream input, OutputStream output)
  141.         throws IOException
  142.         {
  143.             byte[] buffer = new byte[1024]; // Adjust if you want
  144.             int bytesRead;
  145.             while ((bytesRead = input.read(buffer)) != -1)
  146.             {
  147.                 output.write(buffer, 0, bytesRead);
  148.             }
  149.         }
  150.    
  151.     public void toDataBase(String name) throws SQLException, IOException {
  152.         try {
  153.             String Query;
  154.             int s=0;
  155.             while (isFile.read()!=-1)
  156.                 s++;
  157.             System.out.print("Размер файла:");
  158.             System.out.println(s);
  159.             Query = "declare ";
  160.             Query+= "nSYNC_NEW number; ";
  161.             Query+= "begin ";
  162.             Query+= "nSYNC_NEW:=seq_id.nextval ; ";
  163.             Query+= "  insert into audio (sync_id, file_name, blob_data, mime_type) "
  164.                                         + "values(nSYNC_NEW, ?, ?, 'audio/aiff'); ";
  165.             Query+= "  ? := nSYNC_NEW; ";
  166.             Query+= " end;";
  167.        
  168.             CallableStatement cs = conn.prepareCall(Query);
  169.             name="'shit_"+name+"'";
  170.             cs.setString(1, name);
  171.             cs.setBlob(2,isFile,s);
  172.             cs.registerOutParameter(3, java.sql.Types.NUMERIC);
  173.      
  174.             cs.executeUpdate();
  175.      
  176.             long nSYNC_NEW = cs.getLong(3);
  177.             System.out.print("ID обработанной композиции:");
  178.             System.out.println(nSYNC_NEW);
  179.             cs.close();
  180.         }
  181.         catch (Exception e)
  182.         {
  183.             e.printStackTrace();
  184.         }
  185.         finally {
  186.             conn.close();
  187.         }
  188.   }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement