c/s练习——多个客户端上传图片到服务端(tcp)

来源:互联网 发布:注册表关闭445端口 编辑:程序博客网 时间:2024/05/05 00:25

我们使用tcp上传图片,设计思路是:服务端一直开着,客户端上传图片,需要给服务端一个图片的路径,在服务端使用客户端的文件名在保存。如果重名,我们就加一个序号。因为图片上传我们使用的是字节流,所有我们先用一个长度为100的字节数组开保存文件名。然后服务端使用前面一百个数据开获取文件名

下面贴出源码:

客户端:

package com.hsj.net.upload;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;

public class UpLoadPicThreadClient {

    public static void main(String[] args) throws Exception {

        
        System.out.println("请输入你要上传的图片的路径:");        
        Scanner scanner=new Scanner(System.in);
        String filename=scanner.nextLine();
        if(!(filename.endsWith(".jpg")||filename.endsWith(".png")||filename.endsWith(".gif"))){
            throw new RuntimeException("请输入一个图片格式的文件");
        }
        File file=new File(filename);
        if(!file.exists()||!file.isFile()){
            throw new RuntimeException("你输入的不是一个存在的标准的文件");
        }
        if(file.length()>12*1024*1024){
            throw new RuntimeException("只能接受小于5m的图片");
        }
        Socket s=new Socket("128.0.0.47",10010);        
        FileInputStream filein=new FileInputStream(file);
        BufferedInputStream ips=new BufferedInputStream(filein);            
        String fileName=file.getName();
        
        byte[] filearr=Arrays.copyOf(fileName.getBytes(), 1024);
        
        InputStream in=s.getInputStream();
        
        OutputStream out=s.getOutputStream();        
        
        byte[] buf=new byte[1024];
        int len=0;
    
        out.write(filearr);
        
        while((len=ips.read(buf))!=-1){
            out.write(buf, 0, len);
            out.flush();
        }        
        s.shutdownOutput();        
        len=in.read(buf);
        String info=new String(buf,0,len);
        System.out.println(info);
        filein.close();
        ips.close();
        s.close();        
    }

}

==========================================================================================

需要多线程执行的代码,编写一个类实现runnable接口

package com.hsj.net.upload;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ThreadUp implements Runnable {

    private Socket s;

    public ThreadUp(Socket s) {
        this.s = s;
    }

    public void run() {
        try {
            String addr = s.getInetAddress().getHostAddress();
            System.out.println("客户端ip: " + addr);
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            byte[] filearr = new byte[1024];
            len = in.read(filearr, 0, 1024);
            int i = 0;
            for (; i < filearr.length; i++) {
                if (filearr[i] == 0)
                    break;
            }
            String info = new String(filearr, 0, i);
            File file = new File(info);
            int count = 1;
            while (file.exists()) {
                file = new File(info.substring(0, info.lastIndexOf(".")) + "("
                        + (count++) + ")"
                        + info.substring(info.lastIndexOf("."), info.length()));
            }
            BufferedOutputStream ops = new BufferedOutputStream(
                    new FileOutputStream(file));
            while ((len = in.read(buf)) != -1) {
                ops.write(buf, 0, len);
                ops.flush();
            }
            out.write("图片上传成功".getBytes());
            out.flush();
            s.shutdownOutput();
            s.close();
            ops.close();

        } catch (Exception e) {
        }
    }

}

==============================================

服务端代码:

package com.hsj.net.upload;

import java.net.ServerSocket;
import java.net.Socket;
public class UpLoadPicThreadServer {

    public static void main(String[] args) throws Exception {

        ServerSocket ss = new ServerSocket(10010);
        while(true){            
            Socket s = ss.accept();
            new Thread(new ThreadUp(s)).start();
        }
    }

}



原创粉丝点击