Android—基於Socket與上傳圖片到用戶端,

來源:互聯網
上載者:User

Android—基於Socket與上傳圖片到用戶端,

      最近項目中需要用戶端和Socket互相傳遞資料時候需要相互傳遞圖片所以做下總結以免以後忘記,也希望給大家帶來協助。

      先上用戶端的代碼:

      根據圖片名稱上傳照相機中單個照片(此方法為自己封裝)

      參數所代表的是照片的路徑。

 private void upload(String path) {           int length = 0;         byte[] sendBytes = null;         Socket socket = null;         DataOutputStream dos = null;         FileInputStream fis = null;         try {             try {                 socket = new Socket(ip,duankou);                 dos = new DataOutputStream(socket.getOutputStream());                 File file = new File(filename);                 fis = new FileInputStream(file);                 sendBytes = new byte[1024];                 while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {                     dos.write(sendBytes, 0, length);                     dos.flush();                 }             } finally {                 if (dos != null)                     dos.close();                 if (fis != null)                     fis.close();                 if (socket != null)                     socket.close();             }         } catch (Exception e) {             e.printStackTrace();         }    }  

註:在android Activity調用此方法的時候一定要開一個子線程去調用(網路訪問在主線程中是android中一個標誌性的錯誤!!!!)

     接下來是服務端的代碼,測試使用建立一個java類即可

public class Receiver implements Runnable {    public static void main(String[] args) {        try {            final ServerSocket server = new ServerSocket(3333);            Thread th = new Thread(new Runnable() {                public void run() {                    while (true) {                        try {                            System.out.println("開始監聽...");                            Socket socket = server.accept();                            System.out.println("有連結");                            receiveFile(socket);                        } catch (Exception e) {                        }                    }                }            });            th.run(); // 啟動線程運行        } catch (Exception e) {            e.printStackTrace();        }    }    public void run() {    }    public static void receiveFile(Socket socket) {        byte[] inputByte = null;        int length = 0;        DataInputStream dis = null;        FileOutputStream fos = null;        try {            try {                dis = new DataInputStream(socket.getInputStream());                fos = new FileOutputStream(new File("./cc.jpg"));                inputByte = new byte[1024];                System.out.println("開始接收資料...");                while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {                    System.out.println(length);                    fos.write(inputByte, 0, length);                    fos.flush();                }                System.out.println("完成接收");            } finally {                if (fos != null)                    fos.close();                if (dis != null)                    dis.close();                if (socket != null)                    socket.close();            }        } catch (Exception e) {        }    }}

  最後可以在java項目的根目錄下看有沒有成功了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.