Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
oi Pessoal, to precisando fazer uma transferencia de arquivo usando nio, mas como sou iniciante em java.. to tendo serias dificuldades.. mas enfim.. vamos ao problema.. quem puder me ajudar eu agradeço muito :)Bom.. Vi um exemplo e coloquei pra funcionar que era o assim: FileChannel srcChannel = new FileInputStream("arquivo.rar").getChannel(); FileChannel dstChannel = new FileOutputStream("dstFilename.rar").getChannel(); dstChannel.transferFromsrcChannel , 0, srcChannel .size());mas beleza, funciona qndo eu uso no cliente, mas o arquivo ta no servidor, a comunicacao é feita atraves de sockets (SocketChannel), mas como eu posso fazer com que o TransferFrom pegue do SocketChannel.. eu esperava que fosse mais ouu menos assim: SocketChannel sc = null; sc = SocketChannel.open(); FileChannel dstChannel = new FileOutputStream("dstFilename.rar").getChannel(); dstChannel.transferFrom(sc, 0, sc.size());
voce nao pode fazer uma verificação da existencia do arquivo a ser tranferido.... so apos esta consistencia ele vai adiante.At+ http://forum.imasters.com.br/public/style_emoticons/default/joia.gif
:wacko: como?? nao entendi nao...
Client.java
import java.io.;import java.net.;import java.nio.;import java.nio.channels.;import java.nio.charset.;import java.util.regex.;public class Client { private static void query(String host) throws IOException { InetSocketAddress isa = new InetSocketAddress(InetAddress.getByName(host), port); SocketChannel sc = null; try { sc = SocketChannel.open(); FileOutputStream fos = new FileOutputStream("arquivo_transferido.rar"); FileChannel fchannel = fos.getChannel(); fchannel.transferFrom(sc, 0, 1024); fchannel.close(); fos.close(); } finally { if (sc != null) sc.close(); } } public static void main(String[] args) { int firstArg = 0; String s = "dell"; try { query(s); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Server.java
import java.io.;import java.net.;import java.nio.;import java.nio.channels.;import java.nio.charset.;import java.util.;import java.util.regex.*;public class Server { private static int PORT = 8013; private static int port = PORT; private static ServerSocketChannel setup() throws IOException { ServerSocketChannel ssc = ServerSocketChannel.open(); InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port); ssc.socket().bind(isa); return ssc; } private static void serve(ServerSocketChannel ssc) throws IOException { SocketChannel sc = ssc.accept(); try { // Cria a stream para ler o arquivo original FileInputStream fis = new FileInputStream("arquivo.rar"); FileChannel fchannel = fis.getChannel(); fchannel.transferTo(0, fchannel.size(), sc); fchannel.close(); fis.close(); } finally { sc.close(); } } public static void main(String[] args) throws IOException { if (args.length > 1) { System.err.println("Usage: java TimeServer [port]"); return; } // If the first argument is a string of digits then we take that // to be the port number if ((args.length == 1) && Pattern.matches("[0-9]+", args[0])) port = Integer.parseInt(args[0]); ServerSocketChannel ssc = setup(); for (;;) serve(ssc); }}
Fui adaptando um codigo, mas na hora de pegar o conteudo do sockeChannel com o transferFrom, nao deu certo.. pq eu nao tenho o metodo size() pra passar como terceiro parametro :(
voce nao pode fazer uma verificação da existencia do arquivo a ser tranferido.... so apos esta consistencia ele vai adiante.
At+ http://forum.imasters.com.br/public/style_emoticons/default/joia.gif