I have designed a sender-reciever type program where the sender sends a file and the reciever recieves it automatically. The problem is that though I am using byte-based streams,the output in ceratin cases as follows are getting distorted:
[A]: Bitmap images are getting color-inverted.
[B]: Files other than text files are not uploaded properly,like DOCX files open up corrupt.
Here's the code for the sender:
public void transferFile(String file) throws Exception{
FileInputStream fis=new FileInputStream(fileToUpload.getText());
Socket s=null;
s=ss.accept();
OutputStream out=s.getOutputStream();
byte ch[]=new byte[4096];
while(fis.read(ch)!=-1){
out.write(ch,0,ch.length);
}
out.close();
s.close();
}
For the reciever:
public void recieveFile(String fileName) throws Exception{
s=new Socket(controller,1911);
InputStream in=s.getInputStream();
FileOutputStream fos=new FileOutputStream(new File(fileName));
byte ch[]=new byte[4096];
while(in.read(ch)!=-1)
fos.write(ch,0,ch.length);
fos.close();
in.close();
s.close();
}