⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileserver.java

📁 Its video trNSMITT FILE USING JMF
💻 JAVA
字号:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;


public class FileServer extends Thread 
{
	public static final int FILE_SERVER_PORT = 3000;
	
  private ServerSocket myServer;
 
  public FileServer() throws Exception 
  {
    myServer = new ServerSocket(FILE_SERVER_PORT);
    System.out.println("File Server listening on port " + FILE_SERVER_PORT +
    	" for incoming video files.");
    this.start();
  } 

  public void run() 
  {
    while(true) 
    {
      try 
      {
        Socket client = myServer.accept();
        System.out.println("Recieving a new video file from: "+ client.getInetAddress());
        Connect c = new Connect(client);
      } 
      catch(Exception e) 
      {
     		e.printStackTrace();
      }
    }
  }
   
  // Internal class
	class Connect extends Thread 
	{
		private Socket client = null;
		private BufferedInputStream inStream = null;
		private FileOutputStream outStream = null;
		private String fileName; 
		
		public Connect(Socket clientSocket) 
		{
		 	client = clientSocket;
		 	try 
		 	{
				inStream = new BufferedInputStream(client.getInputStream());
		 	} 
		 	catch(Exception e1) 
		 	{
		   	try 
		   	{
		      client.close();
		   	}
		   	catch(Exception e) 
		   	{
		    	System.out.println(e.getMessage());
		   	}
		   	return;
		 	}
		 	this.start();
		}
	
	  
	  public void run() 
	  {
	    try 
	    {
	         //outStream.write(byteArray);
				//  byte[] buffer = new byte[4092]; // 4K
				
				// Get location first so file can be named.  Stops at \n char
				// Example: location = "complab"    fileName = "video/complab.mov"
				StringBuffer fileName = new StringBuffer("video/");
				int c = inStream.read();
				while (c != (int)'\n' && c != -1)
				{
					fileName.append((char)c);
					c = inStream.read();
				}
				fileName.append(".mov");
				
				// Create video directory if it doesn't exist
				File videoDir = new File("video");
				if (!videoDir.isDirectory())
				{
					if (!videoDir.mkdir())
					{
						System.err.println("Error creating a 'video' directory in which to store video files.");
						inStream.close();
						client.close();
						return;
					}
				}
				
				outStream = new FileOutputStream(fileName.toString());
       
				c = inStream.read();
				while (c != -1) 
				{  
					outStream.write(c);
					c = inStream.read();
				}

				// close streams and connections
				inStream.close();
				outStream.close();
				client.close(); 
				
				System.out.println("Saved video file to " + fileName);
			} 
			catch(Exception e) 
			{
				System.out.println("Error incurred while writing socket input to file.");
				e.printStackTrace();
			}     
	  }
	} // end Connect class
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -