import java.io.*;
import java.net.*;
public class FileServer{
public static void main(String[] args){
try{
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
//BufferedWriter br2 = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//String filename = br1.readLine();
//long fiellength = br1.readLine();
FileOutputStream fos = new FileOutputStream("e:/a.txt");
BufferedWriter br2 = new BufferedWriter(new OutputStreamWriter(fos));
String s1 = br1.readLine();
while(s1 != null){
br2.write(s1);
br2.flush();
s1 = br1.readLine();
}
//System.out.println("succeed");
br1.close();
br2.close();
s.close();
ss.close();
}catch(IOException e){}
}
}
------解决思路----------------------
String s1 = br1.readLine();
while(s1 != null){
br2.write(s1);
br2.flush();
s1 = br1.readLine();
}这个换成
String s1 = null;这样试试?
while ((s1 = br1.readLine())!=null) {
br2.write(s1);
br2.flush();
s1 = br1.readLine();
}
------解决思路----------------------
接收文件的话读取输入不要使用readLine方法,这个方法返回的是字符串,而不是byte数组,所以存在很大概率会导致写入的文件与源文件不同。