这是关于android即时通讯工具。即时通讯尝试发送文件,这就是我发送它的方式:
@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;
}
这是我如何从文件接收连接和单独文本(我将“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的服务中接收文件的方式:
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
}
}
在我接收文件连接并将输入流IS转发到文件接收方法的地方,我看到它获取字节但是一旦接收到文件,它就不会收到任何字节。
它使用tcp / ip。
答案 0 :(得分:2)
好的,试试你的方法
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方法中的其他内容作为注释...只需粘贴我建议的代码。
编辑:它来自apache的IOUtils使用它..