Android通过套接字通信发送文件名

时间:2016-03-02 06:22:43

标签: java android sockets android-networking

我能够通过套接字发送文件并在其他端接收。现在我想发送偶数文件名,以便在从socket接收文件和保存文件后,我可以保存文件名。在客户端添加什么以发送文件名和在服务器中添加文件名,感谢提前所有

Client.java

try {

                clientSocket = new Socket(targetIP, port);
                os = clientSocket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);


                InputStream is = clientSocket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                signalActivity("About to start handshake");

                byte[] buffer = new byte[4096];

                FileInputStream fis = new FileInputStream(fileToSend);
                BufferedInputStream bis = new BufferedInputStream(fis);
                // long BytesToSend = fileToSend.length();

                while(true)
                {

                    int bytesRead = bis.read(buffer, 0, buffer.length);

                    if(bytesRead == -1)
                    {
                        break;
                    }

                    //BytesToSend = BytesToSend - bytesRead;
                    os.write(buffer,0, bytesRead);
                    os.flush();
                }
                fis.close();
                bis.close();

                br.close();
                isr.close();
                is.close();

                pw.close();
                os.close();

                clientSocket.close();


            } catch (IOException e) {
            }
            catch(Exception e)
            {

            }

Server.java

try {



            welcomeSocket = new ServerSocket(port);

            while(true && serviceEnabled)
            {

                socket = welcomeSocket.accept();

                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);


                String inputData = "";

//              String savedAs = "WDFL_File_" + System.currentTimeMillis();

//save the original name and extention

                File file = new File(saveLocation, savedAs);

                byte[] buffer = new byte[4096];
                int bytesRead;

                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos);

                while(true)
                {
                    bytesRead = is.read(buffer, 0, buffer.length);
                    if(bytesRead == -1)
                    {
                        break;
                    }
                    bos.write(buffer, 0, bytesRead);
                    bos.flush();

                }

                bos.close();
                socket.close();
                //Start writing to file

            }


        } catch (IOException e) {

        }
        catch(Exception e)
        {

        }

1 个答案:

答案 0 :(得分:0)

您的客户端应首先发送文件名。之后是文件的内容。

服务器应首先读取文件名,以便可以使用相同的文件名保存文件的以下内容。