服务器接收客户端发送的文件。 选择文本文件时,文件的传输和写入成功,但是当选择图像文件时,文件的传输或写入会生成原始文件中找不到的某些字符,从而破坏图像文件。请帮助:)
公共类服务器{
private DatagramSocket socket = null;
private DatagramPacket inPacket;
private DatagramPacket outPacket = null;
private byte[] inBuffer, outBuffer;
private final int port = 50000;
private ChooseDestination cf = new ChooseDestination();
private BufferedWriter bw;
private int receivedNo = 1;
public static void main(String[] args) {
new Server();
}
public Server(){
this.connect();
}
public void connect(){
try{
socket = new DatagramSocket(port);
String fName = "";
while(true){
inPacket = null;
// waiting for input from client
inBuffer = new byte[1024];
inPacket = new DatagramPacket(inBuffer, inBuffer.length);
socket.receive(inPacket);
// get info of client
int source_port = inPacket.getPort();
InetAddress source_address = inPacket.getAddress();
System.out.println("Client: " + source_address + " Port:" + source_port);
// converts packets received to string for content checking
String data = new String(inPacket.getData(), 0, inPacket.getLength());
if(data.contains("filename")){
System.out.println("filename set");
fName = cf.browseFolders() + data.substring(8);
System.out.println("filename: " +fName);
outBuffer = new byte[1024];
outBuffer = ("ok").getBytes();
outPacket = new DatagramPacket(outBuffer, 0, outBuffer.length, source_address, source_port);
socket.send(outPacket);
}
if (data.endsWith("ERROR!")) {
System.out.println("File doesn't exist.\n");
socket.close();
}
else if (!(data.contains("filename"))){
File file = new File(fName);
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}
//true = append file
FileOutputStream fileOuputStream = new FileOutputStream(fName, true);
data = data.trim();
byte[] write = data.getBytes();
fileOuputStream.write(write);
fileOuputStream.close();
// sending acknowledgments formatted as ACKn where n is the packet received
outBuffer = new byte[1024];
outBuffer = ("ACK" + receivedNo + "").getBytes();
outPacket = new DatagramPacket(outBuffer, 0, outBuffer.length, source_address, source_port);
socket.send(outPacket);
receivedNo++;
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
客户端将文件发送到服务器
public class Client {
private DatagramSocket socket = null;
private DatagramPacket inPacket;
private DatagramPacket outPacket = null;
private byte[] inBuffer, outBuffer;
private final int port = 50000;
private int packetNo = 1;
public Client(){
ChooseFile cf = new ChooseFile();
String fName = cf.BrowseFilesView();
this.sendFile(fName);
}
public static void main(String[] args) {
new Client();
}
public void sendFile(String fName){
try {
InetAddress address = InetAddress.getByName("192.168.1.15");
socket = new DatagramSocket();
Path path = Paths.get(fName);
byte[] data = Files.readAllBytes(path);
outBuffer = new byte[1024];
System.out.println(fName);
String[] tag = fName.split("\\\\");
System.out.println(tag[tag.length-1]);
outBuffer = ("filename"+tag[tag.length-1]).getBytes();
outPacket = new DatagramPacket(outBuffer, 0, outBuffer.length, address, port);
socket.send(outPacket);
String s = null;
int i = 0;
int orig = 0;
while(i < data.length){
i = orig;
byte[] send = new byte[1024];
for( int ii = 0; ii < 1024 && i < data.length; ii++, i++){
send[ii] = data[i];
}
outBuffer = new byte[1024];
outBuffer = send;
outPacket = new DatagramPacket(outBuffer, 0, outBuffer.length, address, port);
socket.send(outPacket);
inBuffer = new byte[1024];
inPacket = new DatagramPacket(inBuffer, inBuffer.length);
socket.receive(inPacket);
String ack = new String(inPacket.getData(), 0, inPacket.getLength());
int ackNo = 0;
try{
ackNo = Integer.parseInt(ack.substring(3));
}
catch(Exception e){
e.printStackTrace();
}
if (packetNo == ackNo){
System.out.println(packetNo + " " + ackNo);
orig = i;
packetNo++;
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}