//客户端
package com.heima.lianxi;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Lianxi7 {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws Exception {
File file=getFile();
Socket socket=new Socket("192.168.1.2",22222);
BufferedReader br=
new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps =new PrintStream(socket.getOutputStream());
ps.println(file.toString());
if("存在".equals(br.readLine())){
System.out.println("您上传的内容已存在,请不要重复上传");
}else {
FileInputStream fis =new FileInputStream(file);
byte [] arr =new byte[1024];
int len;
while ((len=fis.read(arr))!=-1){
ps.write(arr,0,len);
socket.close();
fis.close();
}
}
}
public static File getFile() {
Scanner sc= new Scanner(System.in);
while (true){
File file =new File(sc.nextLine());
if(!file.exists()){
System.out.println("您输入的文件不存在");
}else if(file.isDirectory()){
System.out.println("您输入的是文件夹");
}else{
return file;
}
}
}
}
//服务器端
package com.heima.lianxi;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Lianxi8 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(22222);
System.out.println("服务器启动,绑定22222");
while (true){
final Socket socket =ss.accept();
new Thread(){
public void run(){
try {
InputStream is = socket.getInputStream();
BufferedReader br=
new BufferedReader(new InputStreamReader(is));
PrintStream ps =new PrintStream(socket.getOutputStream());
File f =new File("unDate");
f.mkdir();
String nextFile=br.readLine();
File file =new File(f,nextFile);
if(file.exists()){
ps.println("存在");
}else{
ps.print("不存在");
}
FileOutputStream fos =new FileOutputStream(file);
byte[]arr=new byte[1024];
int len;
while ((len=is.read())!=-1){
fos.write(arr,0,len);
}
socket.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
//报错
java.io.FileNotFoundException: unDate\E:\JAVA基础口述总结74条.txt (文件名、目录名或卷标语法不正确。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.heima.lianxi.Lianxi8$1.run(Lianxi8.java:45)
//求大神帮忙看下 我已经晕了
|
|