我正在尝试通过TCP套接字将音频文件发送到计算机上的客户端。调用Server函数时,它会连接到套接字,但会抛出FileNotFoundException并拒绝权限。我在清单中拥有所有必需的权限,而且我真的不知道自己缺少什么。
public class Server extends AsyncTask < File, Void, Void > {
public Void doInBackground(File...soundFile) {
System.out.println("server: " + soundFile);
try (ServerSocket serverSocket = new ServerSocket(6669); FileInputStream in = new FileInputStream(soundFile[0])) {
if (serverSocket.isBound()) {
Socket sender = serverSocket.accept();
OutputStream out = sender.getOutputStream();
byte buffer[] = new byte[2048];
int count;
while ((count = in .read(buffer)) != -1)
out.write(buffer, 0, count);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("server: shutdown");
return null;
}
}
&#13;
在我的MainActivity中调用Server方法。
Uri Selected = data.getData();
wholepath = getRealPathFromURI(Selected);
Toast.makeText(this, wholepath, Toast.LENGTH_LONG).show();
try{
File file = new File(wholepath);
new Server().execute(file);
} catch (Exception e) {
e.printStackTrace();
}
&#13;
感谢所有人给予的帮助。