socket實現圖片上傳

來源:互聯網
上載者:User

實現思路:

在用戶端擷取到檔案流,將檔案流寫入到通過socket指定到某伺服器的輸出資料流中,在伺服器中通過socket擷取到輸入資料流,將資料寫入到指定的檔案夾內,為了提供多使用者同時上傳,這裡需要將在伺服器上傳用戶端的檔案操作放在另開啟一個線程去運行。

完整代碼:

view plain
import java.net.*;
import java.io.*;

/*
服務端將擷取到的用戶端封裝到單獨的線程中。
*/
class  JpgClient2
{
    public static void main(String[] args) throws Exception
    {
        //檢驗檔案
        if(args.length==0)
        {
            System.out.println("指定一個jpg檔案先!");
            return ;
        }
        File file = new File(args[0]);
        if(!(file.exists() && file.isFile() && file.getName().endsWith(".jpg")))
        {
            System.out.println("選擇檔案錯誤,請重新選擇一個正確的檔案。");
            return ;
        }

        //讀取檔案並寫入到伺服器中
        Socket s = new Socket("192.168.137.199",9006);
        FileInputStream fis = new FileInputStream(file);
        OutputStream out = s.getOutputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        while((len=fis.read(buf))!=-1)
        {
            out.write(buf,0,len);
        }

        //通知伺服器發送資料結束
        s.shutdownOutput();

        //擷取伺服器響應
        InputStream in = s.getInputStream();
        byte[] bufIn = new byte[1024];
        int num = in.read(bufIn);
        String str = new String(bufIn,0,num);
        System.out.println(str);

        fis.close();
        s.close();
    }
}

class JpgThread implements Runnable
{
    private Socket s;
    JpgThread(Socket s)
    {
        this.s = s;
    }
    public void run()
    {
        int count = 1;

        String ip = s.getInetAddress().getHostAddress();

        try
        {
            //擷取用戶端資料
            InputStream in = s.getInputStream();

            //指定檔案存放路徑將讀取到客戶提交的資料寫入檔案中
            File dir = new File("c:\\pic");
            File file = new File(dir,ip+"("+count+").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=in.read(buf))!=-1)
            {
                fos.write(buf,0,len);
            }

            //返回上傳狀態給用戶端
            OutputStream out = s.getOutputStream();
            out.write("上傳檔案成功".getBytes());

            fos.close();
            s.close();
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }
}
class  JpgServer2
{
    public static void main(String[] args)throws Exception
    {
        ServerSocket ss = new ServerSocket(9006);

        //開啟線程並發訪問
        while(true)
        {
            Socket s = ss.accept();
            String ip = s.getInetAddress().getHostAddress();
            System.out.println(ip+"....connected");
            new Thread(new JpgThread(s)).start();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.