| | |
Sending files in socket problem///
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 17
Reputation:
Solved Threads: 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:
resiver::
help....
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:
Java Syntax (Toggle Plain Text)
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::
Java Syntax (Toggle Plain Text)
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....
•
•
Join Date: Jul 2008
Posts: 17
Reputation:
Solved Threads: 0
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
resiver:
enjoy
sender
Java Syntax (Toggle Plain Text)
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(); } } }
Java Syntax (Toggle Plain Text)
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(); } }
![]() |
Similar Threads
- Java Socket Problem (Java)
- Perl FTP Problem (Perl)
- socket problem,,,,, plz help (Java)
- Upload problem of large files in Win 2003 (Windows NT / 2000 / XP)
- Problem with My Project (Missing Files) (Visual Basic 4 / 5 / 6)
- WinSock problems. (C++)
- Problem In using Substring.. (Java)
- Outlook Express 6 not sending attachments (Windows Software)
Other Threads in the Java Forum
- Previous Thread: Random.nextInt(); cannot find symbol
- Next Thread: Java SQL update syntax
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying oracle page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string swing system thread threads time tree variablebinding windows xor





