图片上传客户端与服务端

Posted 暮雪超霸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片上传客户端与服务端相关的知识,希望对你有一定的参考价值。

        //一万年太久,只争朝夕
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();
        }

    }

}

 

以上是关于图片上传客户端与服务端的主要内容,如果未能解决你的问题,请参考以下文章

纯Ajax上传图片到服务器端

c#客户端图片上传到服务器端文件并且把路径保存到vs自带数据库

移动端图片操作——上传

微信js-sdk,选择图片,上传,下载到本地,php服务端

请教各位问题:java web客户端上传图片到服务器的D盘下,请问客户端怎么通过http访问图片

用cwRsync多次上传下载文件如何让客户端的文件路径和服务器端的文件路径完全一样。