martes, 29 de mayo de 2012

Eliminar archivo por FTP

private static void borrarArchivoFTP() {

        String pass = "000.000.000.000";//url
        String filename = "/jasper4.pdf";
        String user = "anonymous";
        String server = "000.000.000.000";//url
        FTPClient client = new FTPClient();

        try {
            client.connect(server);
            client.login(user, pass);


// Delete file on the FTP server. When the FTP delete complete
// it returns true.


            boolean deleted = client.deleteFile(filename);
            if (deleted) {
                System.out.println("File deleted...");
            }

            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

Enviar archivo por FTP

private static void envioArchivoFTP2() {
        String pass = "00.000.00.000";//url
        String localPath = "C:/Colores .png";
        String remotePath = "/Colores.png";
        String user = "anonymous";
        String server = "00.000.00.000";//url
        InputStream inp = null;
        try {
            URL url = new URL("ftp://" + user + ":" + pass + "@" + server + remotePath + ";type=i");
            URLConnection urlc = url.openConnection();
            OutputStream os = urlc.getOutputStream();
            File fichero = new File(localPath);
            inp = new FileInputStream(fichero);
            byte bytes[] = new byte[1024];
            int readCount = 0;

            while ((readCount = inp.read(bytes)) > 0) {
                os.write(bytes, 0, readCount);
            }
            os.flush();
            os.close();
            inp.close();


        } catch (Exception ex) {
            ex.printStackTrace();

        }

    }

Borrar archivos de directorio, determinando su caducidad

 private static void borrarArchivoDirectorio() {
        String direccion = "C:\\Users\\asegura\\AppData\\Roaming\\JDeveloper\\system11.1.2.0.38.60.17\\o.j2ee\\drs\\GEDIForms\\GEDIFormsWebWebApp.war\\archivosTemporales";

        File directorio = new File(direccion);
        File f;
        if (directorio.isDirectory()) {
            String[] files = directorio.list();
            if (files.length > 0) {
                System.out.println(" Directorio vacio: " + direccion);
                for (String archivo : files) {
                    System.out.println(archivo);
                    f = new File(direccion + File.separator + archivo);

                    System.out.println("Ultima modificación: " + new Date(f.lastModified()));
                    long Time;
                    Time = (System.currentTimeMillis() - f.lastModified());
                    long cantidadDia = (Time / 86400000);
                    System.out.println("Age of the file is: " + cantidadDia + " days");
                    // Attempt to delete it
                    //86400000 ms is equivalent to one day
                    if (Time > (86400000 * 1) && archivo.contains(".pdf")) {
                        System.out.println("Borrado:" + archivo);
                        f.delete();
                        f.deleteOnExit();
                    }

                }
            }
        }

    }

Números aleaterios

 private static void aleatorio() {
        Integer a;
        Random rand = new Random();

        a = Math.round(rand.nextInt(10000000));
        System.out.println("El numero es:" + a);

    }

Sustitución de caracteres

 private static void sustituir() {
        String cadena = "4-0100-0011";
        String cadena1 = "";
        cadena1 = cadena.replaceAll("-", "").trim();
        System.out.println(cadena);
        System.out.println(cadena1);
    }

Comparar long

 private static void compararLong() {
        Long long1 = new Long("1");
        Long long2 = new Long("0");

        System.out.println(long1.compareTo(long2));

    }

Comparar fechas

private static void compararFechas() {
        Date fechaInicial = new Date();
        Date fechaFinal = new Date();
        System.out.println(fechaInicial);
        System.out.println(fechaFinal);
        if (fechaInicial.compareTo(fechaFinal) > 0) {
            System.out.println("Es mayor");
        }
        System.out.println(fechaInicial.compareTo(fechaFinal));
        if (fechaInicial.getTime() > fechaFinal.getTime()) {
            System.out.println("Es mayor 2");
        } else {
            System.out.println("No lo es.");
        }
    }