Java tutorial
/* * ByteArrayDownloadResult.java * * Copyright (c) 2009-2011 International Integrated System, Inc. * All Rights Reserved. * * Licensed Materials - Property of International Integrated System, Inc. * * This software is confidential and proprietary information of * International Integrated System, Inc. ("Confidential Information"). */ package com.iisigroup.cap.component.impl; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import com.iisigroup.cap.component.Request; import com.iisigroup.cap.component.Result; import com.iisigroup.cap.utils.CapWebUtil; /** * <pre> * * outputNamebrowser?? * outputNamebrowser?? * </pre> * * @since 2011/11/15 * @author iristu * @version * <ul> * <li>2011/11/15,iristu,new * <li>2012/2/3,rodeschen,copy from cap * <li>2013/4/15,iristu,IE7 * </ul> */ @SuppressWarnings("serial") public class ByteArrayDownloadResult extends FileDownloadResult { private byte[] _byteArray = null; private String _outputName; private String _contentType; public ByteArrayDownloadResult() { } public ByteArrayDownloadResult(Request request, byte[] byteArray, String contentType, String outputName) { this._request = request; this._byteArray = byteArray; this._contentType = contentType; this._outputName = CapWebUtil.encodeFileName(_request, outputName); } public ByteArrayDownloadResult(Request request, byte[] byteArray, String contentType) { this._request = request; this._byteArray = byteArray; this._contentType = contentType; } @Override public String getLogMessage() { if (_outputName == null) { return _contentType + " byteArrayDownload complete!!"; } else { return new StringBuffer("Download file:").append(_outputName).toString(); } } public byte[] getByteArray() { return _byteArray; } public String getOutputName() { return _outputName; } public String getContentType() { return _contentType; } @Override public void add(Result result) { if (result instanceof ByteArrayDownloadResult) { ByteArrayDownloadResult r = (ByteArrayDownloadResult) result; this._request = r._request; this._contentType = r._contentType; this._byteArray = r._byteArray; this._outputName = CapWebUtil.encodeFileName(_request, r._outputName); } } @Override public void respondResult(ServletResponse response) { int length = -1; InputStream in = null; OutputStream output = null; try { response.setContentType(getContentType()); response.setContentLength(_byteArray.length); if (getOutputName() != null && response instanceof HttpServletResponse) { HttpServletResponse resp = (HttpServletResponse) response; HttpServletRequest req = (HttpServletRequest) _request.getServletRequest(); String userAgent = req.getHeader("USER-AGENT"); if (StringUtils.contains(userAgent, "MSIE")) { resp.setHeader("Content-Disposition", "attachment; filename=\"" + _outputName + "\""); } else { resp.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + _outputName); } resp.setHeader("Cache-Control", "public"); resp.setHeader("Pragma", "public"); } output = response.getOutputStream(); // Stream to the requester. byte[] bbuf = new byte[1024 * 1024]; in = new ByteArrayInputStream(_byteArray); while ((in != null) && ((length = in.read(bbuf)) != -1)) { output.write(bbuf, 0, length); } output.flush(); } catch (IOException e) { e.getMessage(); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(output); } } }// ~