如何在 Android 中使用最新的 NanoHTTPD 2.3.0 提供 mp3 文件?
How to serve a mp3 file using latest NanoHTTPD 2.3.0 in Android?
我已阅读
最新的 NanoHTTPD 2.3.0 再次不支持代码 return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis)
。
我尝试用 return newFixedLengthResponse(fis)
替换它,但它不正确。我怎样才能做到这一点?谢谢!
public class WhosebugMp3Server extends NanoHTTPD {
public WhosebugMp3Server() {
super(8089);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
String answer = "";
FileInputStream fis = null;
try {
fis = new FileInputStream(Environment.getExternalStorageDirectory()
+ "/music/musicfile.mp3");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis);
}
}
您可以像这样对 InputStream
使用 newChunkedResponse()
方法:
return newChunkedResponse(Status.OK, "audio/mpeg", fis);
此代码工作正常(将您的声音放在资产文件夹中)
class SoundServer extends NanoHTTPD {
SoundServer() {
super(8089);
}
@Override
public Response serve(IHTTPSession session) {
InputStream myInput = null;
try {
myInput = mContext.getAssets().open("sound.mp3");
} catch (IOException e) {
e.printStackTrace();
}
return createResponse(Response.Status.OK, "audio/mpeg", myInput);
}
//Announce that the file server accepts partial content requests
private Response createResponse(Response.Status status, String mimeType,
InputStream message) {
return newChunkedResponse(status, mimeType, message);
}
}
此代码适用于从 nanohttpd 网络服务器提供任何类型的文件。
@Override
public Response serve(IHTTPSession session) {
String msg = "<html><body><h1>Hello server</h1></body></html>\n";
String msgUnauthorized = "<html><body><h1>Unauthorized</h1></body></html>\n";
Method method = session.getMethod();
String uri = session.getUri();
Map<String, String> files = new HashMap<>();
Storage storage = new Storage(OpenRAP.getContext());
String currentpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/";
String temp = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/temp/";
String ecarpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/ecars_files/";
String xcontent = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/xcontent/";
MainActivity.checkfiles();
updateurl();
String Endpoint = session.getUri();
if (Endpoint.equals("/")) {
String answer = "";
try {
// Open file from SD Card
File root = Environment.getExternalStorageDirectory().getAbsoluteFile();
FileReader index = new FileReader(root +
"/www/openrap/index.html");
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch (IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}
else if (uri.equals("/app-debug.apk")) {
File root = Environment.getExternalStorageDirectory();
FileInputStream fis = null;
File file = new File(root.getAbsolutePath() + "/www/resources/app-debug.apk");
String mime = NanoHTTPD.getMimeTypeForFile("app-debug.apk");
Log.d("Path", root.getAbsolutePath());
try {
if (file.exists()) {
fis = new FileInputStream(file);
} else
Log.d("FOF :", "File Not exists:");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK, mime, fis, file.length());
}
我在这里发送 apk 文件,您可以发送具有相应格式和文件名的任何类型的文件
我已阅读
最新的 NanoHTTPD 2.3.0 再次不支持代码 return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis)
。
我尝试用 return newFixedLengthResponse(fis)
替换它,但它不正确。我怎样才能做到这一点?谢谢!
public class WhosebugMp3Server extends NanoHTTPD {
public WhosebugMp3Server() {
super(8089);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
String answer = "";
FileInputStream fis = null;
try {
fis = new FileInputStream(Environment.getExternalStorageDirectory()
+ "/music/musicfile.mp3");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis);
}
}
您可以像这样对 InputStream
使用 newChunkedResponse()
方法:
return newChunkedResponse(Status.OK, "audio/mpeg", fis);
此代码工作正常(将您的声音放在资产文件夹中)
class SoundServer extends NanoHTTPD {
SoundServer() {
super(8089);
}
@Override
public Response serve(IHTTPSession session) {
InputStream myInput = null;
try {
myInput = mContext.getAssets().open("sound.mp3");
} catch (IOException e) {
e.printStackTrace();
}
return createResponse(Response.Status.OK, "audio/mpeg", myInput);
}
//Announce that the file server accepts partial content requests
private Response createResponse(Response.Status status, String mimeType,
InputStream message) {
return newChunkedResponse(status, mimeType, message);
}
}
此代码适用于从 nanohttpd 网络服务器提供任何类型的文件。
@Override
public Response serve(IHTTPSession session) {
String msg = "<html><body><h1>Hello server</h1></body></html>\n";
String msgUnauthorized = "<html><body><h1>Unauthorized</h1></body></html>\n";
Method method = session.getMethod();
String uri = session.getUri();
Map<String, String> files = new HashMap<>();
Storage storage = new Storage(OpenRAP.getContext());
String currentpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/";
String temp = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/temp/";
String ecarpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/ecars_files/";
String xcontent = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/xcontent/";
MainActivity.checkfiles();
updateurl();
String Endpoint = session.getUri();
if (Endpoint.equals("/")) {
String answer = "";
try {
// Open file from SD Card
File root = Environment.getExternalStorageDirectory().getAbsoluteFile();
FileReader index = new FileReader(root +
"/www/openrap/index.html");
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch (IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}
else if (uri.equals("/app-debug.apk")) {
File root = Environment.getExternalStorageDirectory();
FileInputStream fis = null;
File file = new File(root.getAbsolutePath() + "/www/resources/app-debug.apk");
String mime = NanoHTTPD.getMimeTypeForFile("app-debug.apk");
Log.d("Path", root.getAbsolutePath());
try {
if (file.exists()) {
fis = new FileInputStream(file);
} else
Log.d("FOF :", "File Not exists:");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return newFixedLengthResponse(Response.Status.OK, mime, fis, file.length());
}
我在这里发送 apk 文件,您可以发送具有相应格式和文件名的任何类型的文件