不念过往,不为未来,珍惜现在。
单线程处理
package demo01FileUpload; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class TcpServer { public static void main(String[] args) throws IOException { //1、创建一个服务器的serversocket 对象 ServerSocket server= new ServerSocket(8888); //2、等待客户端连接 Socket socket=server.accept(); InputStream is=socket.getInputStream(); // File file= new File("D:\\java学习\\day25"); if(!file.exists()) { file.mkdirs(); } FileOutputStream fos=new FileOutputStream(file+"\\1.jfif"); int len=0; byte[] bytes=new byte[1024]; while((len=is.read(bytes))!=-1) { fos.write(bytes,0,len); } socket.getOutputStream().write("图片上传成功".getBytes()); fos.close(); socket.close(); server.close(); } }
package demo01FileUpload; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; /* * * 实现步骤: * 1、创建一个本地字节收入流FileInputStream对象,构造方法中绑定读取的数据源 * 2、创建一个socket对象,构造方法中绑定服务器的ip地址和端口号 * 3、使用socket中的方法getoutputStream获取网络字节输出流 * 4、使用本地字节输入流中的read方法,读取本地文件 * 5、使用网络输出流中write方法,把读取到的文件上传到服务器 * 6、使用socket中方法,获取输入流对象 * 7、通过网络输入流对象中的read反复,读取回写数据 * 8、释放资源 * * */ public class TcpClient { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("D:\\图片\\123.jfif"); Socket socket =new Socket("127.0.0.1",8888); OutputStream os=socket.getOutputStream(); int len=0; byte[] bytes=new byte[1024]; while((len=fis.read(bytes))!=-1) { os.write(bytes,0,len); } //告诉服务器,文件已经发送完毕 socket.shutdownOutput(); InputStream is=socket.getInputStream(); while((len=is.read(bytes))!=-1) { System.out.println(new String(bytes,0,len)); } //释放资源 fis.close(); socket.close(); } }
多线程
package demo02FileUpload; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Random; public class TcpServer { public static void main(String[] args) throws IOException { //1、创建一个服务器的serversocket 对象 ServerSocket server= new ServerSocket(8887); while(true) { Socket socket=server.accept(); new Thread (new Runnable() { public void run() { try { InputStream is=socket.getInputStream(); File file= new File("D:\\java学习\\day25"); if(!file.exists()) { file.mkdirs(); } String fileName="classFive"+System.currentTimeMillis()+new Random().nextInt(99999)+".jfif"; FileOutputStream fos=new FileOutputStream(file+"\\"+fileName); int len=0; byte[] bytes=new byte[1024]; while((len=is.read(bytes))!=-1) { fos.write(bytes,0,len); } socket.getOutputStream().write("图片上传成功".getBytes()); fos.close(); //2、等待客户端连接 }catch(IOException e) { System.out.println(e); } } }).start(); } } }
web服务器
package demo03; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TcpServer { public static void main(String[] args) throws IOException { ServerSocket server=new ServerSocket(9999); Socket socket=server.accept(); InputStream is=socket.getInputStream(); byte[] bytes=new byte[1024]; int len=0; //while((len= is.read(bytes))!=-1) { //System.out.println(new String(bytes,0,len)); //} BufferedReader br=new BufferedReader(new InputStreamReader(is)); String line=br.readLine(); String [] arr=line.split(" "); String htmlpath =arr[1]; System.out.println(System.getProperty("user.dir")+htmlpath); FileInputStream fis=new FileInputStream(System.getProperty("user.dir")+htmlpath); OutputStream os=socket.getOutputStream(); os.write("HTTP/1.1 20 OK\r\n".getBytes()); os.write("Content-Type:text/html\r\n".getBytes()); os.write("\r\n".getBytes()); while((len=fis.read(bytes))!=-1) { os.write(bytes,0,len); } fis.close(); socket.close(); server.close(); } }
package demo03; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TcpServerThread { public static void main(String[] args) throws IOException { ServerSocket server=new ServerSocket(9999); while(true) { Socket socket=server.accept(); new Thread (new Runnable() { public void run() { try { InputStream is=socket.getInputStream(); byte[] bytes=new byte[1024]; int len=0; //while((len= is.read(bytes))!=-1) { //System.out.println(new String(bytes,0,len)); //} BufferedReader br=new BufferedReader(new InputStreamReader(is)); String line=br.readLine(); //System.out.println(line); String [] arr=line.split(" "); String htmlpath =arr[1]; System.out.println(System.getProperty("user.dir")+htmlpath); //D:\java学习\day25\web\index.html FileInputStream fis=new FileInputStream(System.getProperty("user.dir")+htmlpath); OutputStream os=socket.getOutputStream(); os.write("HTTP/1.1 20 OK\r\n".getBytes()); os.write("Content-Type:text/html\r\n".getBytes()); os.write("\r\n".getBytes()); while((len=fis.read(bytes))!=-1) { os.write(bytes,0,len); } fis.close(); socket.close(); }catch(IOException e) { System.out.println(e); } } }).start(); } } }