quarta-feira, 15 de maio de 2013

JSF: Exibir um popup durante a execução de um evento



<a4j:commandButton
status="waitModalStatus"
title="Enviar arquivo via FTP"
actionListener="#{processoMB.sendFtp(upload)}"
image="/resources/images/16x16/up.png">


<rich:popupPanel
    id="waitModal" style="text-align:center" autosized="true" modal="true" width="200" >
<h:graphicImage library="images" name="ajaxloader32.gif" alt="ai"/><br />
    <h:outputText value="Transmitindo o arquivo, aguarde!" />
</rich:popupPanel>

<a4j:status name="waitModalStatus">
    <f:facet name="start">
        <rich:componentControl event="start" operation="show" target="waitModal" />
    </f:facet>  
    <f:facet name="stop">
        <rich:componentControl event="stop" operation="hide" target="waitModal" />
    </f:facet>
</a4j:status>

segunda-feira, 13 de maio de 2013

Copiar arquivos no Java

Dessa forma o arquivo de destino não é alterado durante a cópia:


  1.  public static void copyFile(File source, File destination) throws IOException {  
  2.      if (destination.exists())  
  3.          destination.delete();  
  4.   
  5.      FileChannel sourceChannel = null;  
  6.      FileChannel destinationChannel = null;  
  7.   
  8.      try {  
  9.          sourceChannel = new FileInputStream(source).getChannel();  
  10.          destinationChannel = new FileOutputStream(destination).getChannel();  
  11.          sourceChannel.transferTo(0, sourceChannel.size(),  
  12.                  destinationChannel);  
  13.      } finally {  
  14.          if (sourceChannel != null && sourceChannel.isOpen())  
  15.              sourceChannel.close();  
  16.          if (destinationChannel != null && destinationChannel.isOpen())  
  17.              destinationChannel.close();  
  18.     }  
  19. }  

Dessa forma o arquivo de destino pode sofrer alterações:

  1. public class Teste {  
  2.     public static void main(String[] args) throws IOException {  
  3.         File arquivoOrigem = new File("c:/temp/original.txt");  
  4.         FileReader fis = new FileReader(arquivoOrigem);  
  5.         BufferedReader bufferedReader = new BufferedReader(fis);  
  6.         StringBuilder buffer = new StringBuilder();  
  7.         String line = "";  
  8.         while ((line = bufferedReader.readLine()) != null) {  
  9.             buffer.append(line).append("\n");             
  10.         }  
  11.           
  12.         fis.close();  
  13.         bufferedReader.close();  
  14.           
  15.         File arquivoDestino = new File("C:/temp/copia.txt");  
  16.         FileWriter writer = new FileWriter(arquivoDestino);  
  17.         writer.write(buffer.toString());  
  18.         writer.flush();  
  19.         writer.close();  
  20.           
  21.           
  22.           
  23.           
  24.     }