Find the answer to your Linux question:
Results 1 to 7 of 7
Hi all I was trying to write a program in java to transfer a file over the LAN. Code: public static void recieve(String filename,Socket s) throws Exception { InputStream in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Jul 2005
    Location
    Chd,India
    Posts
    135

    File transfer script over a LAN


    Hi all
    I was trying to write a program in java to transfer a file over the LAN.

    Code:
    	public static void recieve(String filename,Socket s) throws Exception
    	{
    		InputStream in = s.getInputStream();
    		BufferedReader input = new BufferedReader(new InputStreamReader(in));
    		
    		FileWriter f = new FileWriter(filename);
    		System.out.print("Recieving " + filename + "\n");
    		
    		int MAX = 100;
    		int n;
    		String buff;
    		char line[] = new char[MAX];
    		while( (n = input.read(line,0,MAX)) != -1)
    		{
    			f.write(line,0,n);
    		}
    		f.close();
    		System.out.print("Recieved file\n");
    	}
    
    	public static void transfer_file(String filename,String ip) throws Exception
    	{
    		InetAddress server = InetAddress.getByName(ip);
    		int port = 20000;
    		Socket s = new Socket(server,port);
    
    		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    		OutputStream out = s.getOutputStream(
    );
    		
    		output.write("sending " + filename + "\n");
    		output.flush();
    		InputStream f = new FileInputStream(filename);
    		BufferedReader fin = new BufferedReader(new InputStreamReader(f));
    		
    		int n,MAX = 1000;
    		
    		char line[] = new char[MAX];
    		
    		while(  (n = fin.read(line,0,MAX)) != -1)
    		{
    			output.write(line,0,n);
    			output.flush();
    		}
    		output.close();
    	}
    The problem is that text files are transitted and recieved fine.But images and othe files are not sent or recieved properly.I recieve a file of the same size as the original one but its contents have a lot of chinese charecters that the original one did not.

    Help
    The strong shall live and the weak will die
    In the end,only the fittest survive in this world
    - Shishio Makoto

  2. #2
    Linux Newbie
    Join Date
    Jul 2005
    Location
    Chd,India
    Posts
    135
    I am having the same problem on localhost,so there is no network error i suppose.
    But what could be the problem here?
    The strong shall live and the weak will die
    In the end,only the fittest survive in this world
    - Shishio Makoto

  3. #3
    Linux Newbie
    Join Date
    Jul 2005
    Location
    Chd,India
    Posts
    135
    guys i would definitely appreciate any help.
    The strong shall live and the weak will die
    In the end,only the fittest survive in this world
    - Shishio Makoto

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by gagan_goku
    guys i would definitely appreciate any help.
    Sorry, I'd do it in a shellscript with rsh.

  5. #5
    Just Joined!
    Join Date
    Dec 2005
    Posts
    12

    Text vs Binary

    I'm not a java coder but it looks like you're processing everyting as text. This will allow text to work fine, but with images and other binary files they are corrupted in the transfer.

  6. #6
    Linux User
    Join Date
    Dec 2004
    Posts
    323
    Quote Originally Posted by gagan_goku
    guys i would definitely appreciate any help.
    If you need help fast, then please appreciate that this forum is not Java specific. I you google around for "java forum" there are probably plenty places that are more geared towards your problem.

    Hope this helps

    Tech

  7. #7
    Just Joined!
    Join Date
    Oct 2005
    Location
    Mass
    Posts
    52
    well this is definitely a job for the java forums not linux... i haven't done java in a little while so i can't remember the exact methods you need to use

    your problem resides in the fact that you're trying to transfer 8 bits of data into a 16 bit char.. therefore your binary files don't save correctly..

    keep looking through more read() methods in the java docs and you'll come across one that utilizes charset

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •