Reputation: 38
So I want to send a simple text file from pc (windows) to a android phone. I put together parts of code that I found here on Stackoverflow. My problem is when I send this text file:
testing, sending this text. testing, sending this text.
I get this file on the client side:
’ testing, sending this text. testing, sending this text.
I appreciate any solutions you might have.
ps. I know I can just delete the first characters, but why is this happening ? am I doing something wrong ?
Some more info:
this is my server code (in java):
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer extends Thread {
public static final int SERVERPORT = 8901;
public static void main( String [] args) {
try {
System.out.println("S: Connecting...");
ServerSocket serverSocket = new ServerSocket(SERVERPORT);
System.out.println("S: Socket Established...");
Socket client = serverSocket.accept();
System.out.println("S: Receiving...");
ObjectOutputStream put = new ObjectOutputStream(
client.getOutputStream());
String s = "1.txt";
String str = "C:/";
String path = str + s;
System.out.println("The requested file is path: " + path);
System.out.println("The requested file is : " + s);
File myFile = new File (path);
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = client.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
client.close();
} catch (Exception e) {
System.out.println("S: Error");
e.printStackTrace();
} finally {
}
}
}
and this is my client app (on android):
public class MainActivity extends Activity {
int u;
byte[] aByte = new byte[1];
int bytesRead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread( new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Socket sock = null;
BufferedInputStream bis = null;
// Debug.waitForDebugger();
try {
sock = new Socket("192.168.1.53", 8901);
bis = new BufferedInputStream(sock.getInputStream());
String path = "/mnt/sdcard/2.txt";
FileOutputStream fs = new FileOutputStream(new File(path));
BufferedOutputStream bos = new BufferedOutputStream(fs);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
bytesRead = bis.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = bis.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
sock.close();
} catch (IOException ex) {
// Do exception handling
}
/*
byte jj[] = new byte[1024];
while ((u = get.read(jj, 0, 1024)) != -1) {
fs.write(jj, 0, u);
}
fs.close();
System.out.println("File received");
s.close();*/
} catch (Exception e) {
e.printStackTrace();
// System.exit(0);
}
}
}).start();
}
}
Upvotes: 0
Views: 908
Reputation: 311052
It's caused by the following unused code:
ObjectOutputStream put = new ObjectOutputStream(
client.getOutputStream());
This statements writes an object stream header. Waste of time and space, and corrupts your data as you aren't using ObjectInputStream
at the peer. Delete it.
Apart from that, your code exhibits the usual problems.
byte [] mybytearray = new byte [(int)myFile.length()];
You don't need an array the size of the file: 8192 will do. Also here you are assuming the file size is < 2GB.
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
You don't really need the BufferedInputStream in this application.
bis.read(mybytearray,0,mybytearray.length);
Here you are assuming that read()
filled the buffer. It isn't contracted to do that, only to transfer at least one byte or else return -1.
os.write(mybytearray,0,mybytearray.length);
Ditto. The canonical way to copy streams in Java is as follows:
int count;
byte[] buffer = new byte[8192]; // or whatever you like, anything > 0. Heroic sizes buy you nothing
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
Your client code is even worse. You can reduce it all to exactly the same loop above, with different inputs and outputs. You don't need the ByteArrayOutputStream
at all.
Upvotes: 1
Reputation: 907
My guess is, if you created your text file inside android, then it might be created in UTF format.
But if you created it in windows and copied to your machine, then the format might be different, that's why the extra characters.
You can try to write your text and ensure format is UTF-8 and see if that still happens.
Upvotes: 0