下载⽂件downloadFile public static void downLoadFile(InputStream inStream, String fileName)
{
if (StringUtils.isBlank(fileName)) {
fileName = UUID.randomUUID().toString().replaceAll("-", "");
}
HttpServletRequest req = getRequest();
String agent = Header("User-Agent");
boolean isMSIE = (agent != null) && (agent.indexOf("MSIE") != -1);
try
{
if (isMSIE) {
fileName = de(fileName, "UTF-8");
fileName = place("+", "%20");
} else {
fileName = new Bytes("UTF-8"), "ISO-8859-1");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpServletResponse response = getResponse();
response.setCharacterEncoding("UTF-8");
response.setHeader("content-disposition", "attachment;filename=\"" + fileName + "\"");
response.setContentType("application/octet-stream");
OutputStream outStream = null;
try {
outStream = OutputStream();
byte[] cache = new byte[1024];
int length = ad(cache);
while (length != -1) {
outStream.write(cache, 0, length);
length = ad(cache);
}
} catch (IOException e) {
e.printStackTrace();
try
{
outStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
finally
{
try
{
outStream.flush();
} catch (IOException e) {replaceall()
e.printStackTrace();
}
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static HttpServletRequest getRequest()
{
ActionContext ac = Context();
HttpServletRequest req = null;
if (ac != null) {
req = (("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
}
return req;
}
public static HttpServletResponse getResponse()
{
ActionContext ac = Context();
HttpServletResponse response = (("com.opensymphony.xwork2.dispatcher.HttpServletResponse"); return response;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论