DataServer2(客戶端上傳文件到服務器端)

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class DataServer2 {

        public static final int SERVICE_PORT=1500;
        public static final int DATA_SIZE=1024;
        public static final String FILE_PATH = "C:\\MP4\\";
        public static String FILE_NAME;
        public static void main(String[] args) {
                String filePath = FILE_PATH;
                try{
                        ServerSocket server = new ServerSocket(SERVICE_PORT);
                        System.out.println("Service started");
                        
                        for(;;){
                                Socket nextClient = server.accept();
                                System.out.println("Received request from " + nextClient.getInetAddress()+ ":"+ nextClient.getPort());                               
                                InputStream in = nextClient.getInputStream();
                                writeFile(filePath,in);
                                nextClient.close();
                        }
                        
                }catch(Exception e){
                        e.printStackTrace();
                } 
                
        }

        /**
         * 將客戶端輸入流寫服務器端文件
         * @param filePath	服務器端文件位置
         * @param in 客戶端輸入流
         * @throws Exception
         */
        public static void writeFile(String filePath,InputStream in)throws Exception {
            BufferedReader readerFile = new BufferedReader(
                    new InputStreamReader(in));
		    DataInputStream reader = new DataInputStream(in);
		    String filename=readerFile.readLine();
		    System.out.println("FileName:---------->>>>>>" + filename);
		    getFileName(filename);
		    
            File file=new File(filePath + FILE_NAME);
            if(file.exists()){
                file.delete();
                System.out.println("Create new file");
            }
            FileOutputStream fos = new FileOutputStream(file);
            
		    byte[] bs = new byte[DATA_SIZE];
		    int length;
		    System.out.println("Data receiving started");
		    while( (length=reader.read(bs)) != -1){
		            fos.write(bs,0,length);
		            bs = new byte[DATA_SIZE];
		            Thread.sleep(200);
		    }
		    System.out.println("Receiving completed");
		    readerFile.close();
		    reader.close();
		    fos.close(); 
        }
        
        public static void getFileName(String filename)throws Exception {
        	FILE_NAME = filename == null?"":filename.substring(filename.lastIndexOf("\\") + 1,filename.length());
        	System.out.println("FILE_NAME= " + FILE_NAME);
        }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值