本人新马,基本学完才来逛论坛的,在做网络编程练习之客户端向服务器上传文件时发现自己写的代码运行起来没问题,本地路径能够判断成功,文件也能上传成功,
但我重复上传同一个文件的时候,却没有像老师的程序一样给我返回提示,大家帮忙看看,谢谢!
代码如下:
public class Test2_Client {
/**
* * 客户端向服务器上传文件
*
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(11111);
System.out.println("服务器启动,绑定11111端口");
while (true) {
final Socket socket = server.accept();
new Thread() {
public void run() {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
PrintStream ps = new PrintStream(
socket.getOutputStream());
String fileName = br.readLine();
File dir = new File("update");
dir.mkdir();
File file = new File(dir, fileName);
if (file.exists()) {
ps.println("已存在");
socket.close();
return;
} else {
ps.println("不存在");
}
FileOutputStream fos = new FileOutputStream(file);
int len;
byte[] arr = new byte[8192];
while ((len = is.read(arr)) != -1) {
fos.write(arr, 0, len);
}
fos.close();
socket.close();
} catch (IOException e) {