Java tutorial
/** * Copyright (C) 2012-2014 52North Initiative for Geospatial Open Source * Software GmbH * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 2 as publishedby the Free * Software Foundation. * * If the program is linked with libraries which are licensed under one of the * following licenses, the combination of the program with the linked library is * not considered a "derivative work" of the program: * * - Apache License, version 2.0 * - Apache Software License, version 1.0 * - GNU Lesser General Public License, version 3 * - Mozilla Public License, versions 1.0, 1.1 and 2.0 * - Common Development and Distribution License (CDDL), version 1.0 * * Therefore the distribution of the program linked with libraries licensed under * the aforementioned licenses, is permitted by the copyright holders if the * distribution is compliant with both the GNU General Public License version 2 * and the aforementioned licenses. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package org.n52.sensorweb.series.policy.editor.ctrl; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletResponse; import org.n52.web.BaseController; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.context.ServletContextAware; @Controller public class DownloadController extends BaseController implements ServletContextAware { /** * Size of a byte buffer to read/write file */ private static final int BUFFER_SIZE = 4096; /** * Path of the file to be downloaded, relative to application's directory */ private String permissionsXmlPath = "/WEB-INF/classes/permissions.xml"; private ServletContext sc; /** * Method for handling file download request from client */ @RequestMapping(method = RequestMethod.GET) public void doDownload(HttpServletResponse response) throws IOException { File downloadFile = new File(permissionsXmlPath); FileInputStream inputStream = new FileInputStream(downloadFile); // get MIME type of the file String mimeType = sc.getMimeType(permissionsXmlPath); if (mimeType == null) { // set to binary type if MIME mapping not found mimeType = "application/octet-stream"; } // set content attributes for the response response.setContentType(mimeType); response.setContentLength((int) downloadFile.length()); // set headers for the response String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); response.setHeader(headerKey, headerValue); // get output stream of the response OutputStream outStream = response.getOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; // write bytes read from the input stream into the output stream while ((bytesRead = inputStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } inputStream.close(); outStream.close(); } @RequestMapping(value = "/view", method = RequestMethod.GET) public void viewPermissionsXml(HttpServletResponse response) throws IOException { File downloadFile = new File(permissionsXmlPath); FileInputStream inputStream = new FileInputStream(downloadFile); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); // get output stream of the response OutputStream outStream = response.getOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; // write bytes read from the input stream into the output stream while ((bytesRead = inputStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } inputStream.close(); outStream.close(); } @Override public void setServletContext(ServletContext sc) { this.sc = sc; permissionsXmlPath = sc.getRealPath(permissionsXmlPath); } public void setPermissionsXmlPath(String permissionsXmlPath) { this.permissionsXmlPath = permissionsXmlPath; } }