1个回答
展开全部
你具体指的是JSP吧,其实关于另存对话框显示是浏览器自行决定的,即:一个url的跳转如果浏览器检测到这个url指向的是一个文件流那么就会显出另存为的对话框。具体的方式时前台一个 超链接<a href="url">,url指向后台的servlet(struts对应action,等其他的业务逻辑)。而后台的实现方式:把文件流输出到reponse的输出流中。具体代码为:
public static void downloadFile(HttpServletResponse response,InputStream in,String sFileName)
{
java.io.OutputStream toClient = null;
try
{
// 清空response
response.reset();
// 设置response的Header
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=" + new String(sFileName.getBytes("iso-8859-1")));
//response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2 * 1024];
int iReadLen;
while(-1 != (iReadLen = in.read(buffer)))
{
toClient.write(buffer, 0, iReadLen);
}
toClient.flush();
} catch (Exception e)
{
logger.error(null, e);
}finally
{
IOUtils.closeQuietly(toClient);
}
}
public static void downloadFile(HttpServletResponse response,InputStream in,String sFileName)
{
java.io.OutputStream toClient = null;
try
{
// 清空response
response.reset();
// 设置response的Header
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=" + new String(sFileName.getBytes("iso-8859-1")));
//response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2 * 1024];
int iReadLen;
while(-1 != (iReadLen = in.read(buffer)))
{
toClient.write(buffer, 0, iReadLen);
}
toClient.flush();
} catch (Exception e)
{
logger.error(null, e);
}finally
{
IOUtils.closeQuietly(toClient);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询