iT邦幫忙

0

android如何透過socket上傳圖片到mysql的問題

各位工程師大大們好,小弟最近要寫一支聊天APP,目前做到需要將使用者的照片上傳到mysql。小弟使用的伺服器是用JAVA寫的,而連線套件是使用socket套件做連線和傳輸字串,至於連到mysql是用JDBC套件做連線,但是小弟不知道如何將圖片透過socket上傳到mysql。希望各位工程師大大能給小弟指點迷津,謝謝。

看更多先前的討論...收起先前的討論...
1. server 接收檔案
2. 檔案路徑存入 mysql
3. API 資料以 url 形式回傳 mysql 圖片連結
微笑 iT邦新手 3 級 ‧ 2020-12-24 16:17:58 檢舉
雖然技術不成熟,但如果是我的話應該會用API把照片上傳到imgur,只把url存到mysql
混水摸魚 iT邦研究生 4 級 ‧ 2020-12-24 17:41:21 檢舉
查查 base64 這個關鍵字,另外很少人會把圖直接存在資料庫,你可以app拍照轉成base64字串透過socket 傳到主機然後再將base64轉成圖片,存到主機某個資料夾。
AndyAWD iT邦新手 5 級 ‧ 2020-12-28 00:47:48 檢舉
base64會讓檔案變大,okhttp有支援form表單上傳

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-12-26 11:06:36

提供參考
2014 年的答案
做到「Androdi 透過 socket 上傳圖片,server (java) 接收」而已
剩下要自己補(eg.server 將圖片寫進 mySql)

Server

package com.example.serverlate;
    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.DisplayMetrics;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class ServerLate extends Activity {

        private ServerSocket serverSocket;

        Handler updateConversationHandler;

        Thread serverThread = null;


        private ImageView imageView;//  private TextView text;

        public static final int SERVERPORT = 6000;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_server_late);


            imageView=(ImageView) findViewById(R.id.imageViewServer);//text = (TextView) findViewById(R.id.textView01); 

            updateConversationHandler = new Handler();

            this.serverThread = new Thread(new ServerThread());
            this.serverThread.start();

        }

        @Override
        protected void onStop() {
            super.onStop();
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        class ServerThread implements Runnable {

            public void run() {
                Socket socket = null;
                try {
                    serverSocket = new ServerSocket(SERVERPORT);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while (!Thread.currentThread().isInterrupted()) {

                    try {

                        socket = serverSocket.accept();

                        CommunicationThread commThread = new CommunicationThread(socket);
                        new Thread(commThread).start();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        class CommunicationThread implements Runnable {

            private Socket clientSocket;

            private DataInputStream input;//private BufferedReader input;       

            public CommunicationThread(Socket clientSocket) {

                this.clientSocket = clientSocket;

                try {

                    //this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));

                    InputStream in = this.clientSocket.getInputStream();
                    this.input = new DataInputStream(in);               

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            public void run() {
                System.out.println("hello");
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        byte[] data;//String read = input.readLine();
                        int len= this.input.readInt();                  
                        data = new byte[len];                   
                        if (len > 0) {
                            this.input.readFully(data,0,data.length);
                        }   
                        /*
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        byte[] data;
                        int length = 0;
                        while ((length = this.input.read(data))!=-1) {
                            out.write(data,0,length);
                        }
                           data=out.toByteArray();
                        */

                        updateConversationHandler.post(new updateUIThread(data));//updateConversationHandler.post(new updateUIThread(read));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

        }

        class updateUIThread implements Runnable {
            private byte[] byteArray;//private String msg;

            public updateUIThread(byte[] array){    //public updateUIThread(String str) {
                this.byteArray=array;   //this.msg = str;
            }

            @Override
            public void run() { 
                Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray .length);
                imageView.setImageBitmap(bitmap);//text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
            }
        }
    }

Client

package com.example.clientlate;

    import java.io.BufferedWriter;
    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.nio.ByteBuffer;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Bitmap.CompressFormat;
    import android.graphics.drawable.BitmapDrawable;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;

    public class ClientLate extends Activity {

        private Socket socket;

        private static final int SERVERPORT = 5000;
        private static final String SERVER_IP = "10.0.2.2";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_client_late);      

            new Thread(new ClientThread()).start();
        }

        public void onClick(View view) {
            try {           
                ImageView imageView=(ImageView) findViewById(R.id.imageView1);//EditText et = (EditText) findViewById(R.id.EditText01);
                Bitmap bmp=((BitmapDrawable)imageView.getDrawable()).getBitmap(); //String str = et.getText().toString();

                ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
                bmp.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); 
                byte[] array = bos.toByteArray();

                OutputStream out = socket.getOutputStream(); 
                DataOutputStream dos = new DataOutputStream(out);
                dos.writeInt(array.length);
                dos.write(array, 0, array.length);

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        class ClientThread implements Runnable {

            @Override
            public void run() {

                try {
                    InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

                    socket = new Socket(serverAddr, SERVERPORT);

                } catch (UnknownHostException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }

        }
    }

非常感謝海綿寶寶大大的提供的方法,想要再問問,目前小弟是想要在server端新增加接收檔案的線呈,但是苦於目前的伺服器接收用的while迴圈只有一個,容易被客戶端傳輸影響,如果想要再寫個接收圖片或檔案的線呈要如何寫,小弟的server端方法如大大提供的server端方法一樣,想請問大大能不能幫小弟指點迷津,謝謝

我要發表回答

立即登入回答