首页 > 代码库 > 图片上传客户端与服务端

图片上传客户端与服务端

        //一万年太久,只争朝夕
package
uploadImg; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; /*建立Socket服务 * 读取文件上传源 * 获取输出流。将数据发送到服务端 * 告诉服务端数据发送完毕。服务端停止读取 * 读取服务端发回的数据 * * * * * */ public class ImgClient { public static void main(String[] args) throws UnknownHostException, IOException { Socket s = new Socket("127.0.0.1", 10009); //建立Socket服务 //加载目标文件 FileInputStream file = new FileInputStream("c:\\0.jpg"); //获取Socket输出流 OutputStream outputStream = s.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; //将目标文件读入byte数组 while ((len = file.read(buf)) != -1) { outputStream.write(buf, 0, len); } s.shutdownOutput(); BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream())); String readLine = bufIn.readLine(); System.out.println(readLine); bufIn.close(); file.close(); s.close(); } }
package uploadImg;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ImgServer {
    /*
     * 建立ServerSockte服务 获取Sockte对象 Socket输入流 创建文件存储 多线程。并发 返回反馈结果到客户端 结束流
     */
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10009);
        while (true) {
            Socket accept = ss.accept();
            new Thread(new UploadImg(accept)).start();
        }
    }

}
package uploadImg;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.io.OutputStream;
import java.io.PrintWriter;

public class UploadImg implements Runnable {
    private static final int SIZE = 1024 * 1024 * 2;
    private Socket s;

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

    @Override
    public void run() {

        int count = 0;
        String ip = s.getInetAddress().getHostName();
        System.out.println(ip + "....." + "connect");
        try {
            InputStream inputStream = s.getInputStream();

            File dir = new File("c:\\pic");
            if (!dir.exists()) {
                dir.mkdir();
            }
            File file = new File(dir, ip + ".jpg");
            while (file.exists()) {
                file = new File(dir, ip + "(" + (count++) + ").jpg");

            }
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buf)) != -1) {
                fos.write(buf, 0, len);
                if (file.length() > SIZE) {
                    System.out.println(ip + "文件体积太大");
                    fos.close();
                    s.close();
                    System.out.println(ip + "..." + file.delete());
                    return;
                }
            }
            PrintWriter pw = new PrintWriter(s.getOutputStream());
            pw.println("上传成功");
            fos.close();
            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

 

图片上传客户端与服务端