服务器无法通过套接字从客户端接收文件

时间:2013-03-07 10:48:05

标签: android sockets client-server

我想从客户端向服务器发送文件,但在进程结束时我只在服务器上获得一个空文件。我不知道为什么我会遇到这个问题,请告诉我是否有任何人在我的代码中出错,或者对这个问题有适当的解决方案。 提前致谢 我的服务器和客户端代码是:

服务器端代码:

    int bytesRead;
    int current = 0;
    File myFile = new File("/sdcard/seCopy.txt");

    byte [] mybytearray  = new byte [666];
    InputStream is = input_stream;//assigning the input stream value to
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;

  try {
      is = socketObj.getInputStream();
      fos = new FileOutputStream(myFile);
      bos = new BufferedOutputStream(fos);
      int count;

      while ((count = is.read(mybytearray)) > 0) 
      {
          bos.write(mybytearray, 0, count);
      }
      String decoded = new String(mybytearray, "UTF-8");
      sendField.setText("Stored successfuly " +decoded);
      bos.flush();
      bos.close();
      is.close();
      }
  catch (FileNotFoundException ex) 
         { Toast.makeText(this, "fIle not found exception",Toast.LENGTH_LONG).show(); }
  catch(Exception obj)
         { Toast.makeText(this, "fIle not found exception",Toast.LENGTH_LONG).show();  }

客户端代码:

    File myFile = new File("/sdcard/FileInputOutput/se.txt");//My file path, please 

            byte [] mybytearray  = new byte [(int)myFile.length()];
           try
            {
            FileInputStream fis = new FileInputStream(myFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
            int count;
            while ((count = bis.read(mybytearray)) > 0) 
            {
                out.write(mybytearray, 0, count);
            }

            out.flush();
            out.close();
            fis.close();
            bis.close();
            }
            catch (IOException e) 
            {  e.printStackTrace(); }
       } });

0 个答案:

没有答案