Skip to content

Instantly share code, notes, and snippets.

@dcode
Forked from cr0t/gist:1243431
Created May 4, 2014 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcode/f879a62574e94a2265ae to your computer and use it in GitHub Desktop.
Save dcode/f879a62574e94a2265ae to your computer and use it in GitHub Desktop.
object Download extends Controller {
import org.apache.commons.logging.Log
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs._
import java.io.InputStream
import java.net.URI
import java.net.URLDecoder
def download(filename: String) = {
try {
val uri = play.configuration("hadoop.uri") // "hadoop.uri=hdfs://hadoop" in conf/application.conf file
val conf = new Configuration()
val hdfs = FileSystem.get(URI.create(uri), conf)
response.setHeader("Content-type", "application/force-download")
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"")
// response.setHeader("Content-Length", filesize + "");
response.direct = hdfs.open(new Path(filename)).asInstanceOf[java.io.InputStream]
}
catch {
case e: Exception => Logger.error(e, "Couldn't get file from Hadoop by the given filename")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment