Java tutorial
/** * Copyright 2015 Q24 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.kahu.hawaii.util.call.http.response; import java.io.IOException; import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; public class FileDownload { private final String fileName; private final String mimeType; private final HttpServletResponse response; public FileDownload(final String fileName, final String mimeType, final HttpServletResponse response) { this.fileName = fileName; this.mimeType = mimeType; this.response = response; } public void writeHttpStatus(final int statusCode) { response.setStatus(statusCode); } public void writeHeaders() { if (StringUtils.isNotEmpty(mimeType)) { response.setContentType(mimeType); } if (StringUtils.isNotEmpty(fileName)) { response.setHeader("Content-Disposition", "attachment; filename=" + fileName); } } public OutputStream getOutputStream() throws IOException { return response.getOutputStream(); } public String getMimeType() { return mimeType; } public void close() throws IOException { response.getOutputStream().flush(); response.getOutputStream().close(); } }