DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: How to transfer images and other files across network

  1. #1
    Join Date
    May 2005
    Posts
    12

    How to transfer images and other files across network

    How do i transfer images and other files across the network? i've tried the FileInputStream and getting the output stream from the socket and writing to it but it messes up the contents of the file. for some reason it seems to work pretty well for text and doc files but messes up for images and others. could somebody tell me whats wrong? thanks...

  2. #2
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    I move files over the network with Java with no problem regardless of type. Just make sure your code doesn't make some assumptions about file type. Basically treat everything the same -- as binary. Now if you're attempting to implement some higher level protocol (like FTP) then you can have some confusion and trouble related to ascii/binary modes. Without seeing at least some little snippets of your input and output loops it's tough to say.

  3. #3
    Join Date
    May 2005
    Posts
    12
    sorry yea i should have posted some code.

    this is to send the file. its a generic as possible. i just wanna send files between two computers, no ftp and all that. plain old tcp.

    Code:
    private void uploadFile ( PrintWriter outputStream )
                throws IOException
        {
            FileInputStream fileStreamer = null;
            try
            {
                fileStreamer = new FileInputStream ( fileToUpload );
                int c;
    
                while ( ( c = fileStreamer.read () ) != -1 )
                {
                    outputStream.write ( c );
                    outputStream.flush ();
                }
            }
    and this is getting the file from the network
    Code:
    public void downloadFile () throws IOException
        {
            InputStream inputStream = null;
            DataOutputStream output = null;
    
            try
            {
                inputStream = this.socket.getInputStream ();
                output = new DataOutputStream ( new FileOutputStream ( destinationFile ) );
                int c;
    
                while ( ( c = inputStream.read () ) != -1 )
                {
                    output.write ( c );
                    this.setBytesRead ( new Long ( bytesRead++ ) );
                    output.flush ();
                }
            }
    thats about the major parts. see anything wrong in it? it seems to send text and bmp files properly but not anything else.

  4. #4
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    PrintWriter outputStream
    I suspect this is the problem. PrintWriter expects to be writing text so it may be doing end of line translations, which in general will screw you up any time 0x0d or 0x0a values are encountered.

  5. #5
    Join Date
    May 2005
    Posts
    12
    ok so i've made it not printwriter but it still scrambles up all the images and stuff. i've made just a client and server program.

    Client
    Code:
    public class client
    {
        public static void main ( String[] args ) throws IOException
        {
            File file = new File ( args[0] );
            FileInputStream fileStream = new FileInputStream ( file );
            Socket socket = new Socket ( "laptop", 8080 );
            OutputStream out = socket.getOutputStream ();
    
            int c;
            while ( ( c = fileStream.read () ) != -1 )
            {
                out.write ( c );
                
            }
            out.flush ();
        }
    }
    Server
    Code:
    public class server
    {
        public static void main ( String[] args )
        {
            File file = new File ( "c:\\kobe.jpg" );
            try
            {
                ServerSocket socket = new ServerSocket ( 8080 );
                Socket s = socket.accept ();
                BufferedReader input = new BufferedReader ( new InputStreamReader ( s.getInputStream () ) );
                DataOutputStream out = new DataOutputStream ( new FileOutputStream ( file ) );
    
                int c;
                while ( ( c = input.read () ) != -1 )
                {
                    out.write ( c );
                    //System.out.print(c);
                }
                out.flush ();
            }
            catch ( IOException e )
            {
            }
        }
    }
    it still doesnt work. could you post your solution to this or a sample program that you've done that works please? i'm really at a fix here.

  6. #6
    Join Date
    May 2005
    Posts
    12
    ok nm i got it. have to wrap a dataoutputstream around the output and input streams

  7. #7
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    Excellent!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center