62,620
社区成员
发帖
与我相关
我的任务
分享import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Birth_A_TestFile {
public static void main(String[] args){
File f1 = new File ("D:/TestTransmission");
File f2 = new File ("D:/TestTransmission","Test.txt");
try {
f1.mkdir ();
f2.createNewFile ();
FileOutputStream fos = new FileOutputStream (f2);
fos.write ("Hello".getBytes ());
fos.close ();
} catch (IOException e) {
e.printStackTrace();
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
ServerSocket ss = null;
Socket s = null;
boolean started = false;
public static Server server = new Server ();
public static void main(String[] arg0){
server.launch ();
}
public void choose(){
DataOutputStream dos_choose =null;
try {
dos_choose = new DataOutputStream(s.getOutputStream ());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println ("您可以进行三项操作:" + "\r\n" + " 1.发送文件" + "\r\n" + " 2.关闭客户端主机"+"\r\n"+" 3.关闭服务器"+"\r\n"+"**************注意:输入其他视为关闭服务器**************");
Scanner in = new Scanner (System.in);
int i = in.nextInt ();
if (i == 1) {
try {
dos_choose.writeUTF ("**************服务器准备开始发送文件!**************");
} catch (IOException e) {
e.printStackTrace();
}
new Thread (new Transmission ()).start ();
} else if (i == 2) {
try {
dos_choose.writeUTF ("**************服务器将要关闭您的主机!**************");
} catch (IOException e) {
e.printStackTrace();
}
new Thread (new ShutDown ()).start ();
} else if (i == 3) {
System.exit (0);
}
}
public void launch(){
try {
ss = new ServerSocket (8888);
started = true;
} catch (IOException e) {
e.printStackTrace ();
}
try {
s = ss.accept ();
System.out.println ("A Client Connected!");
this.choose ();
} catch (IOException e) {
e.printStackTrace ();
}
}
public class Transmission implements Runnable {
private boolean send = false;
private DataInputStream dis = null;
private DataOutputStream dos = null;
public void run(){
try {
File f1 = new File ("D:/TestTransmission","Test.txt");
dis = new DataInputStream (s.getInputStream ());
dos = new DataOutputStream (s.getOutputStream ());
dos.writeUTF ("是否要接收文件? Yes/No" + "\r\n" + "(输入其他字符视为不接收)");
String str = dis.readUTF ();
if (str.equals ("Yes")) {
System.out.println (str);
System.out.println ("请输入ok确认发送文件");
Scanner in = new Scanner (System.in);
String str2 = in.nextLine ();
if (str2.equals ("ok")) {
FileInputStream fis2;
try {
fis2 = new FileInputStream (f1);
int c = 0;
while ((c = fis2.read ()) != -1) {
dos.write ((char) c);
}
System.out.println("**************文件发送完毕**************");
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
}
} else {
System.out.println ("**************对方不想接收文件**************");
}
} catch (IOException e) {
e.printStackTrace ();
}finally{
choose();
}
}
}
private class ShutDown implements Runnable{
DataOutputStream dos_ShutDown = null;
DataInputStream dis_ShutDown = null;
public void run(){
try {
dis_ShutDown = new DataInputStream (s.getInputStream ());
dos_ShutDown = new DataOutputStream(s.getOutputStream ());
String str = dis_ShutDown.readUTF ();
if(str!=null){
dos_ShutDown.writeUTF ("shutdown -s -t 5");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
public static Client client = new Client ();
public static void main(String[] args){
client.connect ();
}
public void connect(){
try {
s = new Socket ("127.0.0.1",8888);
System.out.println ("Connected!");
client.choose ();
} catch (UnknownHostException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
}
public void choose(){
DataInputStream dis_choose = null;
try {
dis_choose = new DataInputStream (s.getInputStream ());
System.out.println (dis_choose.readUTF ());
} catch (IOException e) {
e.printStackTrace ();
}
System.out.println ("您可以进行三项操作:" + "\r\n" + " 1.接收文件" + "\r\n" + " 2.关闭客户端" + "\r\n" + " 3.接受服务器操作" + "\r\n" + "**************注意:输入其他视为关闭客户端**************");
Scanner in = new Scanner (System.in);
int i = in.nextInt ();
if (i == 1) {
new Thread (new Receive ()).start ();
} else if (i == 2) {
System.exit (0);
} else {
new Thread(new ServerCommand()).start ();
}
}
private class Receive implements Runnable {
File f1 = new File ("D:/TestDownload");
File f2 = new File ("D:/TestDownload","Test.txt");
public void run(){
try {
dos = new DataOutputStream (s.getOutputStream ());
dis = new DataInputStream (s.getInputStream ());
System.out.println (dis.readUTF ());
Scanner in = new Scanner (System.in);
String str = in.nextLine ();
dos.writeUTF (str);
if (str.equals ("Yes")) {
try {
f1.mkdir ();
f2.createNewFile ();
FileOutputStream fos = new FileOutputStream (f2);
int e;
boolean bool = true;
while (bool) {
if((e=dis.read ())!=-1){
fos.write ((char) e);
}
else bool = false;
}
fos.close ();
} catch (SocketException e) {
System.out.println ("*******服务器关闭了!*******");
System.exit (0);
} catch (IOException e) {
e.printStackTrace ();
} finally {
dis.close ();
dos.close ();
}
}
} catch (IOException e) {
e.printStackTrace ();
} finally {
choose ();
}
}
}
private class ServerCommand implements Runnable{
DataInputStream dis_ServerCommand = null;
DataOutputStream dos_ServerCommand = null;
public void run(){
try {
dos_ServerCommand = new DataOutputStream(s.getOutputStream ());
dis_ServerCommand = new DataInputStream(s.getInputStream () );
dos_ServerCommand.writeUTF ("可以发送命令了");
String str =dis_ServerCommand.readUTF ();
String cmd = str;
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec (cmd);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}