我使用netty官方示例HttpStaticFileServerHandler作为文件服务器,但是当我从服务器下载文件时遇到问题,我从服务器下载的mp4文件不完整并且无法显示。
这是我的客户端代码:
FileOutputStream fos = null;
try {
URL website = new URL("http://localhost:8090/export/App/***.mp4");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
fos = new FileOutputStream("/Users/mine/***.mp4");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (Exception e) {
System.out.println("error msg:\n" + e);
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException ioe) {
System.out.println("fos close fail:\n" + ioe);
}
}
最佳答案
确保使用 fos.close()
关闭 FileOutputStream
。
不这样做意味着只会写入部分数据,其他程序在访问该文件时可能会遇到问题。
您可以检查的另一件事是查看文件的文件大小,两侧应该匹配,如果文件太小,请使用文本编辑器打开它,然后查看内容以检查线索。
关于java - Netty HttpStaticFileServerHandler问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53500128/