Add return method for byte[].

This commit is contained in:
Christian P. MOMON 2013-10-27 21:51:03 +01:00
parent 59ffb2530a
commit 750268fe54
2 changed files with 64 additions and 1 deletions

View file

@ -21,6 +21,6 @@
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/commons-fileupload-1.3.jar"/>
<classpathentry kind="lib" path="lib/commons-fileupload-1.3.jar" sourcepath="lib/commons-fileupload-1.3-sources.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View file

@ -529,6 +529,69 @@ public class SimpleServletDispatcher extends HttpServlet
}
}
/**
*
*/
static public void returnFile(final HttpServletResponse response, final String fileName, final byte[] data, final String mimeType, final ContentDispositionType contentDisposition,
final int cacheAge) throws IOException
{
if (data == null)
{
logger.warn("data is null.");
}
/*
* response.setContentType("application/" + extension);
* response.setContentLength((int) data.length);
* response.setHeader("Content-Disposition"
* ,"attachment; filename=\"" + filename + "\"");
* response.flushBuffer();
*/
response.reset();
response.setContentType(mimeType);
response.setContentLength(data.length);
String contentDispositionToken;
if (contentDisposition == ContentDispositionType.ATTACHMENT)
{
contentDispositionToken = "attachment";
}
else
{
contentDispositionToken = "inline";
}
response.setHeader("Content-Disposition", contentDispositionToken + "; filename=\"" + fileName + "\"");
response.setDateHeader("Expires", new Date().getTime() + cacheAge * 1000);
response.setHeader("Cache-Control", "max-age=" + cacheAge);
response.flushBuffer();
ServletOutputStream out = response.getOutputStream();
try
{
out.write(data, 0, data.length);
}
catch (IOException exception)
{
exception.printStackTrace();
response.sendError(HttpServletResponse.SC_PARTIAL_CONTENT);
}
finally
{
try
{
out.flush();
out.close();
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
}
/**
*
*/