Article View: borland.public.jbuilder.java.language
Article #15706Re: How Can I send File by Socket?
From: Jason Burton
Date: Thu, 13 May 1999 00:00
Date: Thu, 13 May 1999 00:00
37 lines
2157 bytes
2157 bytes
> Hello Now i can create tcp socket connect between Server and client ,
> but i dont know how can i send file by socket please tell me. i need
> your help. :-)
Here's some code I use to send a file from a Java server to a Delphi client,
though it will work to send a file to a client written in any language
(assuming the client is implemented in a compatible manner):
BufferedOutputStream binOutput = new
BufferedOutputStream(socket.getOutputStream());
FileInputStream inputStream = new
FileInputStream(fileName); // read in the file
int numBytes = inputStream.available();
int bytesRead = 0;
while (bytesRead < numBytes)
{
int currBytesRead = inputStream.read(fileBuffer);
bytesRead += currBytesRead;
binOutput.write(fileBuffer, 0, currBytesRead);
binOutput.flush();
}
/* I use three 255 chars to signal to the client that the file is done. I
am not sure if this works in all cases, but I have never had any problems
with it. */
for (int i = 0; i < 3; i++)
binOutput.write(255);
binOutput.flush();
As I said, this is just the sending side. I have never implemented the
receiving part in Java, though it would follow roughly the same logic. See
the java.io package to pick which stream classes to use (probably some sort
f BufferedInputStream).
Jason
Technical Details
Message-ID:
<7hel8k$t7021@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3739469C.9183EF89@yahoo.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,326b273374e5f03a X-Google-Attributes: gidf74b3,public From: "Jason Burton" <Jason.Burton@McLeodSoftware.com> Subject: Re: How Can I send File by Socket? Date: 1999/05/13 Message-ID: <7hel8k$t7021@forums.borland.com> X-Deja-AN: 477415508 References: <3739469C.9183EF89@yahoo.com> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Newsgroups: borland.public.jbuilder.java.language