play2 export file from mongodb
(play framework2 从mongdb中直接读取文件字节)不废话,直接上代码
字节来源需要注意:
(1)guava方法:
import com.google.common.io.ByteStreams;
byte[] content = ByteStreams.toByteArray(new FileInputStream(path));
(2)apache方法(未亲测):
import org.apache.commons.io.IOUtils;
byte[] content = IOUtils.toByteArray(new FileInputStream(path));
action:
public Result getFile(String id){
models.File file = models.File.find().byId(id);
byte[] content = file.content;
InputStream input = new ByteArrayInputStream(content);
return ok(input).as("image/png");
}
扔个参考: