br.org.ipti.guigoh.util.DownloadService.java Source code

Java tutorial

Introduction

Here is the source code for br.org.ipti.guigoh.util.DownloadService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.org.ipti.guigoh.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author ipti004
 */
public class DownloadService {

    public static void downloadFileFromURL(String filePath, String fileType)
            throws MalformedURLException, IOException {
        File file = new File(filePath);
        URL url = new URL(!filePath.contains("http") ? "http://" + filePath : filePath);
        FileUtils.copyURLToFile(url, new File(filePath));
        downloadFile(file, fileType);
    }

    public static synchronized void downloadFile(File file, String mimeType) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext context = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) context.getResponse();
        response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
        response.setContentLength((int) file.length());
        response.setContentType(mimeType);
        try {
            OutputStream out;
            try (FileInputStream in = new FileInputStream(file)) {
                out = response.getOutputStream();
                byte[] buf = new byte[(int) file.length()];
                int count;
                while ((count = in.read(buf)) >= 0) {
                    out.write(buf, 0, count);
                }
            }
            out.flush();
            out.close();
            facesContext.responseComplete();
        } catch (IOException ex) {
            System.out.println("Error in downloadFile: " + ex.getMessage());
        }
    }
}