Add return method for byte[].
This commit is contained in:
parent
59ffb2530a
commit
750268fe54
2 changed files with 64 additions and 1 deletions
|
@ -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>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue