2016-07-31 9 views
0

SERVER(ファイルの受信)はJavaでソケット上で適切にソケットを介しMP3やJPEGファイルを送信し

 String ip=JOptionPane.showInputDialog(this,"Enter Port Number :"); 
     String folder=jTextField2.getText(); 
     try 
     { 
     ServerSocket sskt= new ServerSocket(Integer.parseInt(ip)); 
Socket skt= sskt.accept(); 
InputStream is= skt.getInputStream(); 



byte[] bytes= new byte[1024*16]; 

DataInputStream dis=new DataInputStream(skt.getInputStream()); 
     String ext=dis.readUTF(); 

    System.out.println("extension read"); 



String path=folder+"/file."+ext; 
JOptionPane.showMessageDialog(this, path); 
File f= new File(path); 
OutputStream output = new FileOutputStream(f); 
while(is.read(bytes)>0) 
{ 
    output.write(bytes); 
System.out.println("byte read"); 
} 
System.out.println("Done!!!"); 

} 
catch(Exception e) 
{ 
System.out.println(e); 
    } 

CLIENT(ファイルの送信)

 String ip=JOptionPane.showInputDialog(this,"enter server ip:"); 
     String port=JOptionPane.showInputDialog(this,"enter port number :"); 
     File file=new File(jTextField1.getText()); 
     String name=file.getName(); 
     String n= name.substring(name.lastIndexOf(".") + 1); 



     try { 
      Socket skt=new Socket(ip, Integer.parseInt(port)); 
      OutputStream os= skt.getOutputStream(); 
      DataOutputStream dos= new DataOutputStream(os); 
      dos.writeUTF(n); 
      os.flush(); 


      FileInputStream fis= new FileInputStream(file); 
      BufferedInputStream bis= new BufferedInputStream(fis); 
      byte[] mybytearray = new byte[(int) file.length()]; 
      bis.read(mybytearray, 0, mybytearray.length); 
      os.write(mybytearray, 0, mybytearray.length); 
      JOptionPane.showMessageDialog(this,"Done!!"); 
      os.close(); 
     } 

     catch (Exception ex) { 
      System.out.println(ex); 
     } 

私はmp3 BTファイルのすべてのフォーマットを転送することができますよjpegファイルが正しく開かれません。 read()によって返された長さを無視:メディアプレイヤーはそれがクライアントによって送信

誰もがこの

答えて

0

通常の問題で私を助けてくださいすることができたものと同じであるサーバーで作成したファイルのサイズBT任意のmp3ファイルを再生することはできません。あなたのコピーループは両方のようになります。

while ((count = is.read(bytes)) > 0) 
{ 
    output.write(bytes, 0, count); 
} 

あなたは正しいサイズのバッファを割り当てるについて、クライアントにすべてのことゴミを必要としません。

+0

ありがとうございます!私はあなたの努力に感謝します –

関連する問題