🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.java
1 messages
1 total messages Started by konrad Fri, 16 Sep 2011 11:59
przesyłanie pliku jpg (gniazda)
#152855
Author: konrad
Date: Fri, 16 Sep 2011 11:59
133 lines
4682 bytes
Uruchomiłem serwer i klienta do wysyłania plików. Po wysłaniu pliku rozmiar 
się zgadza jednak nie plik nie da się otworzyć nie jest JPG, zauważyłem że 
progam zastępuje pewne znaki spacjami. Proszę o podpowiedź jakieś kodowanie ?
fragmęt SERWERa
public void run()
   {  
      try
      {  
         try
         {
        	 InputStream inStream = incoming.getInputStream();
            OutputStream outStream = incoming.getOutputStream();
      
            int bytesRead;
            int current = 0;
            
            InputStreamReader x = new InputStreamReader(inStream);//"CP1250",
           BufferedReader in = new BufferedReader(x);
           
           System.out.println("0");
           
          // String KOD = in.readLine();
           
           
    
    
           byte [] mybytearray  = new byte [300000];
           
       
           FileOutputStream a =new FileOutputStream(this.NumerPliku());
           BufferedOutputStream bos = new BufferedOutputStream(a);
           System.out.println("3");
     
           current =0;
           System.out.println("5");
    
           do {
               bytesRead = inStream.read(mybytearray, current,1 );//
(mybytearray.length-current)
               if(bytesRead >= 0) current += bytesRead;
               System.out.println(bytesRead);
               System.out.println("5x"+mybytearray[current-1]);
           } while(bytesRead > -1);
    
           System.out.println("7");
           
           System.out.println(current);
           bos.write(mybytearray, 0 , current);
           
           System.out.println("8");
           bos.flush();      
           bos.close();
        
          
       }
            
     
    
     
            finally
            {
               incoming.close();
            }
         }
         catch (IOException e)
         {  
            e.printStackTrace();
         }
      }

      private Socket incoming;
      private int counter;
   }

I KLIENTA

import java.io.*;
import java.net.*;
 
public class Klient {
 
    public static void main(String[] args) { 
        try {
            Socket s = new Socket("localhost", 8189);
            ps = new PrintStream(s.getOutputStream());
            os = s.getOutputStream();
 
            String nazwaPliku = "ek.jpg";
            File f = new File(nazwaPliku);
 
            if (f.exists()) {
                int dlugoscPliku = (int) f.length();
                byte[] tablicaBajtow = new byte[dlugoscPliku];
 
                fis = new FileInputStream(f);
                bis = new BufferedInputStream(fis);
 
                bis.read(tablicaBajtow, 0, dlugoscPliku);
 
                ps.println(nazwaPliku);
                ps.println(dlugoscPliku);
                os.write(tablicaBajtow);
        }
        else
            System.out.println("plik nie istnieje");
        }
        catch (UnknownHostException ex) {
            System.out.println(ex);
        }
        catch (IOException ex) {
            System.out.println(ex);
        }
        finally {
            try {
                fis.close();
                bis.close();
                ps.close();
                os.close();
            }
            catch (IOException ex) {
                System.out.println(ex);
            }
        }
    }
 
    private static OutputStream os;
    private static FileInputStream fis;
    private static BufferedInputStream bis;
    private static PrintStream ps;
}

-- 
Wysłano z serwisu OnetNiusy: http://niusy.onet.pl
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads