Advertisement
Guest User

Untitled

a guest
May 25th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.85 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;
  52.     private long retNum;
  53.     private String MimeType;
  54.     private String fileName;
  55.     private boolean _controlErrors;
  56.     private InputStream isFile;
  57.     private String outFile;
  58.     private InputStream filteredFile;
  59.     private int size;
  60.    
  61.    
  62.    public static void main(String[] args) {
  63.    
  64.     Filter(int syncID){
  65.        _blobId=syncID;
  66.        MimeType="audio/aiff";
  67.        size=0;
  68.     }
  69.    
  70.     public void setFilteredFile(InputStream is){
  71.         filteredFile=is;
  72.     }
  73.    
  74.     public InputStream getFilteredFile(){
  75.         return filteredFile;
  76.     }
  77.    
  78.     public String getMimeType(){
  79.         return MimeType;
  80.     }
  81.    
  82.     public String getFileName(){
  83.         return fileName;
  84.     }
  85.    
  86.     public long getRetNum(){
  87.         return retNum;
  88.     }
  89.    
  90.     public InputStream getIsFile(){
  91.         return isFile;
  92.     }
  93.     public InputStream getfilteredFile(){
  94.        
  95.         return filteredFile;
  96.     }
  97.     public Connection getConnection(){
  98.         return conn;
  99.     }
  100.    
  101.     public void initConnection() throws SQLException, NamingException, ClassNotFoundException {
  102.         Class.forName("oracle.jdbc.driver.OracleDriver");
  103.         conn = DriverManager.getConnection(
  104.                 "jdbc:oracle:thin:@localhost:1521:ORCL", "Mokona",
  105.                 "Mokona");
  106.         conn.setAutoCommit(false);
  107.  
  108.     }
  109.     public void initFile() throws SQLException {
  110.         boolean wasQuery;
  111.         try (PreparedStatement ps = conn.prepareStatement("select t.blob_data, t.file_name from audio t where t.sync_id = ?")) {
  112.             ResultSet rs;
  113.             Blob blob;
  114.             wasQuery = false;
  115.            
  116.             while(!wasQuery && _blobId < 10000){
  117.                 ps.setLong(1, _blobId);
  118.                 rs = ps.executeQuery();
  119.                 System.out.println(ps.toString());
  120.                 while (rs.next()) {
  121.                    
  122.                     blob = rs.getBlob(1);
  123.                     isFile = blob.getBinaryStream();
  124.                    
  125.                     fileName = rs.getString(2);
  126.                     wasQuery = true;
  127.                 }
  128.                 rs.close();
  129.             }
  130.         }
  131.         if (!wasQuery) {
  132.             throw new RuntimeException("Строка с sync_id=" + Long.toString(_blobId) + " не найдена.");
  133.         }
  134.         //return fileName;
  135.     }
  136.    
  137.     public static void copyStream(InputStream input, OutputStream output)
  138.         throws IOException
  139.         {
  140.             byte[] buffer = new byte[1024]; // Adjust if you want
  141.             int bytesRead;
  142.             while ((bytesRead = input.read(buffer)) != -1)
  143.             {
  144.                 output.write(buffer, 0, bytesRead);
  145.             }
  146.         }
  147.    
  148.     public void toDataBase(String name, InputStream is) throws SQLException, IOException {
  149.         try {
  150.             String Query;
  151.            
  152.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  153.             copyStream(is,baos);
  154.            
  155.             size=baos.size();
  156.             System.out.print("Размер файла:");
  157.             System.out.println(size);
  158.            
  159.             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  160.            
  161.             Query = "declare ";
  162.             Query+= "nSYNC_NEW number; ";
  163.             Query+= "begin ";
  164.             Query+= "nSYNC_NEW:=seq_id.nextval ; ";
  165.             Query+= "  insert into audio (sync_id, file_name, blob_data, mime_type) "
  166.                                         + "values(nSYNC_NEW, ?, ?, 'audio/aiff'); ";
  167.             Query+= "  ? := nSYNC_NEW; ";
  168.             Query+= " end;";
  169.        
  170.             CallableStatement cs = conn.prepareCall(Query);
  171.             name="'shit_"+name+"'";
  172.             cs.setString(1, name);
  173.             cs.setBlob(2,bais,size);
  174.             cs.registerOutParameter(3, java.sql.Types.NUMERIC);
  175.      
  176.             cs.executeUpdate();
  177.      
  178.             long nSYNC_NEW = cs.getLong(3);
  179.             System.out.print("ID обработанной композиции:");
  180.             System.out.println(nSYNC_NEW);
  181.             retNum = nSYNC_NEW;
  182.             cs.close();
  183.         }
  184.         catch (Exception e)
  185.         {
  186.             e.printStackTrace();
  187.         }
  188.         finally {
  189.             conn.close();
  190.         }
  191.   }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement