0

hi,
im working on java on some client server project...
in the project the client and the server can send files, but im failng to do so, the resiver get only part of the file...
here is my code
sender:

public void uploadByteFile(String fileNameToGet, File fileDst)
	{

		try
		{

			FileReader fileReader = new FileReader(new File("c:\\7.mpg"));

			BufferedReader bufferedFileReader = new BufferedReader(fileReader);
		//	PrintWriter streamWriter = new PrintWriter(client.getOutputStream());
			DataOutputStream streamWriter = new DataOutputStream(client.getOutputStream());
	
			String line = null;
			PrintWriter outTextStream = new PrintWriter(client
					.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(client
					.getInputStream()));

			outTextStream.println("start?");

			if (in.readLine().matches("start"))
			{
				streamWriter.flush();
				// streamWriter.flush();
				while ((line = bufferedFileReader.readLine()) != null)
				{
					streamWriter.writeBytes(line);
			//		System.out.println(line);
				}
				out.flush();
			}

			//in.close();
		}
		catch (IOException e)
		{
			System.out.println("Error reading from file: " + fileNameToGet);
		}
		finally
		{
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (IOException e)
				{
					System.out.println("Error wile trying to close a file: "
							+ fileDst.getName());
					e.printStackTrace();
				}
			}
		}
	}

resiver::

FileOutputStream out = null;

		try
		{
			PrintWriter outTextStream = new PrintWriter(socket
					.getOutputStream(), true);
			BufferedReader inTextStream = new BufferedReader(
					new InputStreamReader(socket.getInputStream()));

			out = new FileOutputStream("c:\\91.mpg");

			int received = 0;
			byte[] buff = new byte[1024];
			int sum = 0;

			inTextStream.readLine();
			outTextStream.println(("start"));
			DataInputStream in = new DataInputStream(socket.getInputStream());
		
			while ((received = in.read(buff)) > 0)
			{
				out.write(buff, 0, received);
				System.out.println(sum += buff.length);
			}
			out.flush();
			out.close();
			in.close();

			in.close();
			// out = new FileOutputStream("");
			out.close();
			out.flush();
			out = null;

		}
		catch (IOException e)
		{
			System.out.println("Error reading from file: ");
		}
		finally
		{
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (IOException e)
				{
					System.out.println("Error wile trying to close a file: ");
					e.printStackTrace();
				}
			}

		}

help....

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by stewie griffin
1

After long worm hours (the air conditioner is broken) I found the problem the sender and the reviser was using different type of streams I fixed it .. .and the code is attached
enjoy


sender

try
		{

		
			FileInputStream bufferedFileReader = new FileInputStream(
					"c:\\7.mpg");

			DataOutputStream streamWriter = new DataOutputStream(client
					.getOutputStream());

			String line = null;
			int line1 = 0;
			PrintWriter outTextStream = new PrintWriter(client
					.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(client
					.getInputStream()));

			outTextStream.println("start?");

			if (in.readLine().matches("start"))
			{
				streamWriter.flush();
			
				byte[] buff = new byte[1024];
				while ((line1 = bufferedFileReader.read(buff, 0, 1024)) > -1)
				{
					streamWriter.write(buff);
					System.out.println(buff);

				}
				out.flush();
			}

			// in.close();
		}
		catch (IOException e)
		{
			System.out.println("Error reading from file: " + fileNameToGet);
		}
		finally
		{
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (IOException e)
				{
					System.out.println("Error wile trying to close a file: "
							+ fileDst.getName());
					e.printStackTrace();
				}
			}
		}

resiver:

try
		{
			PrintWriter outTextStream = new PrintWriter(socket
					.getOutputStream(), true);
			BufferedReader inTextStream = new BufferedReader(
					new InputStreamReader(socket.getInputStream()));

			out = new FileOutputStream("c:\\91.mpg");

			int received = 0;
			byte[] buff = new byte[1024];
			int sum = 0;

			inTextStream.readLine();
			outTextStream.println(("start"));
			DataInputStream in = new DataInputStream(socket.getInputStream());
		
			while ((received = in.read(buff)) > -1)
			{	
			
				out.write(buff, 0, received);
			//	System.out.println(sum += received);
			}
			out.flush();
			out.close();
			in.close();


		}
		catch (IOException e)
		{
			System.out.println("Error reading from file: ");
		}
		finally
		{
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (IOException e)
				{
					System.out.println("Error wile trying to close a file: ");
					e.printStackTrace();
				}
			}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.