在Android文件传输 [英] FILE TRANSFER in android

查看:210
本文介绍了在Android文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于Android的即时通讯软件。我尝试发送一个文件,这是我如何把它:

this is regarding android instant messenger. im trying to send a file, and this is how i send it :

@Override
public boolean sendFile(String path,String ip, int port) {
// TODO Auto-generated method stub


try {

String[] str = ip.split("\\.");

byte[] IP = new byte[str.length];

for (int i = 0; i < str.length; i++) {

    IP[i] = (byte) Integer.parseInt(str[i]);


}
Socket socket = getSocket(InetAddress.getByAddress(IP), port);
if (socket == null) {
    Log.i("SO sendFILE","null");

    return false;
}

Log.i("SocketOP", "sendFILE-1");
File  f = new File(path);


BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );

FileInputStream fileIn = new FileInputStream(f);
Log.i("SocketOP", "sendFILE-2");




byte [] buffer  = new byte [1024];
    int bytesRead =0;
    while ((bytesRead = fileIn.read(buffer)) > 0) {
        out.write(buffer, 0, bytesRead);
        System.out.println("SO sendFile" + bytesRead);
    }
out.flush();
out.close();
fileIn.close();
Log.i("SocketOP", "sendFILE-3");






    } catch (IOException e) {
        return false;           
        //e.printStackTrace();
    }
    //  Toast.makeText(this, "Lvbvhhging...", Toast.LENGTH_SHORT).show();

    return true;        
}

这是我收到的文件连接和独立文本(我串连文到输出流文本)

this is how i receive connection and seperate text from file (i concatenate "text" to the output stream for the text)

public ReceiveConnection(Socket socket) 
        {
            this.clientSocket = socket;
            this.fileSocket=socket;
            SocketOperator.this.sockets.put(socket.getInetAddress(), socket);
        }

        @Override
        public void run() {
             try {
    //          PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                // PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                    BufferedReader in = new BufferedReader(
                               new InputStreamReader(
                                        clientSocket.getInputStream()));
                     InputStream is=clientSocket.getInputStream();




                    String inputLine;
                    while ((inputLine = in.readLine()) != null)                  {



                         if (inputLine.contains("Text") == true)
                         {


                            appManager.messageReceived(inputLine);  
                    Log.i("SocketOP","text");}

                         else  if 
                        (inputLine.contains("Text") == false)
                        {

                            Log.i("SocketOP","filee");
                             appManager.fileReceived(is);

                         }



                    else{

                         clientSocket.shutdownInput();
                         clientSocket.shutdownOutput();
                         clientSocket.close();

                         fileSocket.shutdownInput();
                         fileSocket.shutdownOutput();
                         fileSocket.close();
                         SocketOperator.this.sockets.remove(clientSocket.getInetAddress());
                         SocketOperator.this.sockets.remove(fileSocket.getInetAddress());

                         Log.i("SocketOP", "CLOSING CONNECTION");
                     }                       
            }       

            } catch (IOException e) {
                Log.e("ReceiveConnection.run: when receiving connection ","");
        }           
        }   
    }

这是怎么我终于收到文件名为imService服务:

and this is how i finally receive the file in a service called imService :

public void fileReceived(InputStream is)
    throws FileNotFoundException, IOException {
    Log.i("IMSERVICE", "FILERECCC-1");


    if (is!= null) {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream("/sdcard/chats/ffffff.txt");
            bos = new BufferedOutputStream(fos);
            byte[] aByte = new byte[1024];
            int bytesRead = 0;
            System.out.println("imService fileReceive" + bytesRead);

            while ((bytesRead = is.read(aByte)) != -1) {
                bos.write(aByte, 0, bytesRead);
                System.out.println("imService fileReceive" + bytesRead);

            }
            bos.flush();
            bos.close();
            Log.i("IMSERVICE", "FILERECCC-2");

        } catch (IOException ex) {
            // Do exception handling
        }
    }

身在何方我收到的文件连接和转发的InputStream是把文件接收方法,我看到它的获得字节,但一旦filereceived被称为它不是receving任何字节。

right where i receive the file connection and forward the inputstream IS to the file receive method, i see that its getting the bytes but once filereceived is called it isnt receving any bytes.

它使用TCP / IP。

it uses tcp/ip.

推荐答案

好吧,试试你的方法。

public void fileReceived(InputStream is)
 throws FileNotFoundException, IOException 
  {
    string result; 
   FileOutputStream fos = null;        
    fos = new FileOutputStream("/sdcard/chats/ffffff.txt"); 

   Log.i("IMSERVICE", "FILERECCC-1");       
 if (is!= null) 
{  

 // result = convertStreamToString(is);              
 // result = result.replace("\n", ""); 
 // Log.e("InputStream output",result);


  //IOUtils.copy(is,fos);

   byte[] buffer = new byte[1024]; 
     int length;     
   while ((length = is.read(buffer)) > 0)
             {     
       fos.write(buffer, 0, length);     
   }      
      // Close the streams     
       fos.flush();     
       fos.close();     
        is.close();     


}     

} 

  /*    public static String convertStreamToString(InputStream is) 
  throws Exception 
   {      
   BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    StringBuilder sb = new StringBuilder();     
     String line = null;     
      while ((line = reader.readLine()) != null) 
              {         
             sb.append(line + "\n");     
              }     
            is.close();     
       return sb.toString(); 
       } 
   */

编辑:让其他的东西在fileReceived方法评论..只是贴上我建议的code

make other stuff in fileReceived method as comment.. just paste my suggested code.

编辑:来自Apache的一个IOUtils使用它。

its a IOUtils from apache use it..

这篇关于在Android文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆