zoukankan      html  css  js  c++  java
  • JSP下载服务器文件

    <%@page import="java.io.FileInputStream"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@page language="java" contentType="text/html; charset=UTF-8"%>
    <%@page import="com.ecc.emp.core.Context" %>
    <%@page import="java.io.File" %>
    <%@ include file="http://www.cnblogs.com/head.jsp"%>
    
    <%
        boolean loadFlag = false;
        ServletOutputStream ou = null;
        FileInputStream fis = null;
        try{    
            Context context=(Context)request.getAttribute("context");
            String accountFilePath = (String)context.getDataValue("accountFilePath");
            //String accountFilePath ="E:/ComeBack/Acc_BOS_CM/WebContent/upload/product/1336873410414custormerStatement.txt";
            //String accountFileName = (String)context.getDataValue("accountFileName");
            String displayFileName = (String)context.getDataValue("displayFileName");
            //String displayFileName ="custormerStatement.txt";
            String fullPath =accountFilePath;
            response.reset();
               response.setContentType("charset=UTF-8");
            response.setContentType("text/HTML");
            //response.setContentType("application/vnd.ms-excel");        
            String filename =  new String(displayFileName.getBytes("gb2312"), "ISO8859-1");
            response.setHeader("Content-Disposition","attachment; filename=" + filename);
            //解决HTTPS不能下载的问题
            response.setHeader("Cache-Control","public");        
            ou =  response.getOutputStream();
            File file = new File(fullPath);
            fis = new FileInputStream(file);        
            try {
                if (fis != null) {
                    int filelen = fis.available();
                    byte a[] = new byte[filelen];
                    fis.read(a);
                    ou.write(a);
                    ou.flush();
                }            
                loadFlag=true;
            } catch (Exception e) {
                System.out.println("Excel is not exist!");
            }
            out = pageContext.pushBody();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                fis.close();
                ou.close();
                ou = null;
                response.flushBuffer();
            } catch (Exception e) {
                // TODO 自动生成 catch 块
                e.printStackTrace();
            }
        }
            
    %>
  • 相关阅读:
    计算机网络中的码元的理解
    屏幕扩展,屏幕相对位置的设置
    wireshark使用入门
    Http下载文件的登录验证
    正则-连续相同的单词
    文件系统和数据库索引用B树而不是红黑树的原因
    红黑树的突破点
    Win 10 Revit 2019 安装过程,亲自踩的一遍坑,有你想要的细节
    Java拦截器的实现原理
    根据进程数,资源数判断是否发生死锁
  • 原文地址:https://www.cnblogs.com/xiaxinggege/p/2565423.html
Copyright © 2011-2022 走看看