发送端 - 它在 android 中,它是 AsyncTask、doInBackground 的一部分。基本上,它将文件名、大小和图像数据发送到服务器端。
InetAddress serverIP = InetAddress.getByName(IP);
Socket client = new Socket(serverIP,SERVER_PORT);
System.out.println("Connected to Server\n");
System.out.println(imgFile.length());
//sending name of the file
PrintWriter name = new PrintWriter(new OutputStreamWriter(client.getOutputStream()),true);
name.println(fileName);
name.flush();
//sending size of the file
name.println(imgFile.length());
name.flush();
//sending body
DataInputStream imgBodyIn = new DataInputStream(new FileInputStream(imgFile));
DataOutputStream imgBodyOut = new DataOutputStream(client.getOutputStream());
byte[] buffer = new byte[BUFFER_SIZE];
int len;
long filesize = imgFile.length();
while(filesize >=0&& (len=imgBodyIn.read(buffer,0,(int)Math.min(buffer.length,filesize)))>0){
imgBodyOut.write(buffer,0,len);
filesize-=len;
}
name.close();
imgBodyIn.close();
imgBodyOut.flush();
imgBodyOut.close();
client.close();
接收方
//receiving name
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String name = in.readLine();
System.out.println("Name of the File : " + name+".JPG");
//receiveing size info
String size = in.readLine();
System.out.println("Size of the File :"+size +"bytes");
//storing file
File f = new File("/home/ubuntu", name+".JPG");
FileOutputStream output = new FileOutputStream(f);
int len=0;
long received=0;
byte[] buf = new byte[BUFFER];
while(received<=Long.parseLong(size)&&(len=sock.getInputStream().read(buf))>0)
{
output.write(buf,0,len);
received +=len;
System.out.print("Receiving.."+received +"/"+size+"\r");
}
output.flush();
output.close();
in.close();
System.out.println("\n"+name+".JPG received");
System.out.println("Size received :"+f.length()+"bytes");
当我尝试发送文件时,会传输正确的大小信息和名称。但是,我无法收到完整的文件;缺少一些字节。我使用的缓冲区大小是 1024。
样品运行:
Waiting for client to connect..
Client Accepted
Name of the File : P1011474.JPG
Size of the File :714438bytes
Receiving..712997/714438
P1011474.JPG received
Size received :712997bytes
socket closed