Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pdf;
- import java.awt.Desktop;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.sql.Blob;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import static java.util.logging.Logger.global;
- import javax.swing.JOptionPane;
- import org.omg.CORBA.portable.InputStream;
- import softwaremecanica.conectar;
- /**
- *
- * @author SASAGA
- */
- public class RecuperarPdf extends javax.swing.JFrame {
- /**
- * Creates new form RecuperarPdf
- */
- public void BorrarFicheros(){
- String SourcePath="C:\\Program Files\\EMILIANO\\pdf";
- File path = new File(SourcePath);
- File[] ficheros = path.listFiles();
- File f=null;
- if(path.exists()){
- for(int x=0;x<ficheros.length;x++){
- f=new File(ficheros[x].toString());
- f.delete();
- }
- }else{
- }
- }
- public void abrirArchivo(){
- try{
- File folder = new File("C:\\Program Files\\EMILIANO\\pdf");
- String pathname = folder+"\\"+Codigo.getText()+".pdf";
- File path = new File(pathname);
- Desktop.getDesktop().open(path);
- }catch(IOException e){
- e.printStackTrace();
- System.out.println("Ruta erronea");
- }
- }
- public void bajar_archivo()throws SQLException, IOException{
- conectar cc=new conectar();
- Connection cn=cc.conexion();
- Statement st=cn.createStatement();
- String sql = "SELECT HojaVida FROM documentacion WHERE CodigoUsuario="+Codigo.getText()+"";
- ResultSet rs=st.executeQuery(sql);
- try {
- rs.next();
- File folder = new File("C:\\Program Files\\EMILIANO\\pdf");
- folder.mkdir();
- String pathname = folder+"\\"+Codigo.getText()+".pdf";
- File file = new File(pathname);
- FileOutputStream output = new FileOutputStream(file);
- Blob archivo = rs.getBlob(1);
- java.io.InputStream inStream = archivo.getBinaryStream();
- int lenght = -1;
- int size =(int)archivo.length();
- byte[]buffer = new byte[size];
- while((lenght = inStream.read(buffer))!=-1){
- output.write(buffer,0,lenght);
- output.close();
- }
- } catch (Exception ioe) {
- throw new IOException(ioe.getMessage());
- }
- }
- public RecuperarPdf() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jButton1 = new javax.swing.JButton();
- Codigo = new javax.swing.JTextField();
- jButton2 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jButton1.setText("Visualizar");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- Codigo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- CodigoActionPerformed(evt);
- }
- });
- jButton2.setText("Salir");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(109, 109, 109)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(46, 46, 46)
- .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(35, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGap(37, 37, 37)
- .addComponent(Codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(79, 79, 79)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(107, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- // TODO add your handling code here:
- bajar_archivo();
- } catch (SQLException ex) {
- Logger.getLogger(RecuperarPdf.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(RecuperarPdf.class.getName()).log(Level.SEVERE, null, ex);
- }
- abrirArchivo();
- }
- private void CodigoActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- BorrarFicheros();
- dispose();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(RecuperarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(RecuperarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(RecuperarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(RecuperarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new RecuperarPdf().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JTextField Codigo;
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement