Estou com um problema para fazer transferencia de arquivo via
socket ...
Quando estou transferindo uma imagem ele zera a imagem e nao remonta !
Estou enviando .. se alguem puder ajudar
import java.io.*;
import java.net.*;
public class FileDownloadClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
//PrintWriter out = null;
FileOutputStream out = null;
BufferedReader in = null;
InputStream inStream=null;
int bytesCopied = 0;
byte[] buffer = new byte[4096];
int bytes;
String host = "10.0.73.21";
String port = "9999";
try {
echoSocket = new Socket("127.0.0.1", 9999);
// out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader
(echoSocket.getInputStream()));
out = new FileOutputStream("c:\\hand.jpg");
System.out.println("Tentando se conectar via TCP no servico"
+ host +
":" + port + " ...");
System.out.println("Conexao estabelecida !");
if ( (inStream = echoSocket.getInputStream()) != null) {
System.out.println("trabalhando ....");
}
while ( (bytes = inStream.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
bytesCopied += bytes;
}
}
catch (UnknownHostException e) {
System.err.println("Não consegui resolver host name :" +
e.getMessage());
}
catch (IOException e) {
System.err.println("Ocorreu erro na comunicacao :" +
e.getClass().getName() + ":" + e.getMessage
());
}
catch (NullPointerException e) {
System.err.println("Arquivo null pointer error:" + e.getClass
().getName() +
":" + e.getMessage());
}
catch (SecurityException e) {
System.err.println("Tge security manager recusou a permissao
para se "
+ "conectar no servico remoto TCP :"
+ e.getMessage());
}
}
}
-----------------------------
import java.io.*;
import java.net.*;
public class FileDownloadServer {
public static void main(String[] arg) throws IOException {
int bytecount = getInputStream();
System.out.println(bytecount + " bytes copiados");
System.exit(0);
}
public static int getInputStream() throws IOException {
FileInputStream input = null;
Socket csocket = null;
BufferedWriter out = null;
BufferedReader buffer = null;
String line;
ServerSocket server = null;
Socket srSocket = null;
PrintWriter outer = null;
try {
server = new ServerSocket(9999);
srSocket = server.accept();
input = new FileInputStream("c:\\hand2.jpg");
buffer = new BufferedReader(new InputStreamReader(input));
if (buffer == null) {
System.out.println("Nao consigue resgatar o arquivo");
}
else {
outer = new PrintWriter(srSocket.getOutputStream(), false);
System.out.println("Enviando dados para o Cliente");
while ( (line = buffer.readLine()) != null) {
outer.write(line);
}
outer.flush();
}
}
catch (Exception e) {}
finally {
srSocket.close();
}
return 1;
}
}
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/jug-petropolis/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/