Example usage for io.vertx.core.http.impl MimeMapping getMimeTypeForExtension

List of usage examples for io.vertx.core.http.impl MimeMapping getMimeTypeForExtension

Introduction

In this page you can find the example usage for io.vertx.core.http.impl MimeMapping getMimeTypeForExtension.

Prototype

public static String getMimeTypeForExtension(String ext) 

Source Link

Usage

From source file:com.sibvisions.vertx.HttpServer.java

License:Apache License

/**
 * Handles a download request.//from  w  ww. j  a  v  a 2s  .  co  m
 * 
 * @param pRequest the request
 */
private void handleDownload(HttpServerRequest pRequest) {
    String sKey = pRequest.params().get("KEY");

    if (sKey == null) {
        pRequest.response().setStatusCode(HttpResponseStatus.BAD_REQUEST.code());
        pRequest.response().end();

        return;
    }

    IFileHandle fh = (IFileHandle) ObjectCache.get(sKey);

    HttpServerResponse response = pRequest.response();

    String sType = MimeMapping.getMimeTypeForExtension(FileUtil.getExtension(fh.getFileName()));

    if (sType != null) {
        response.putHeader(HttpHeaders.CONTENT_TYPE, sType);
    }

    response.putHeader("Content-Disposition", "attachment; filename=\"" + fh.getFileName() + "\"");

    int iLen;

    byte[] byContent = new byte[4096];

    try {
        response.putHeader(HttpHeaders.CONTENT_LENGTH, "" + fh.getLength());

        InputStream in = fh.getInputStream();

        Buffer buffer;

        while ((iLen = in.read(byContent)) >= 0) {
            buffer = Buffer.buffer();
            buffer.appendBytes(byContent, 0, iLen);

            response.write(buffer);
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    response.end();
}