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();
            }
        }
            
    %>
  • 相关阅读:
    vue-router路由
    前端路由与后端路由
    getsupportfragmentmanager 没有这个方法
    Glide使用
    Android使用Glide加载Gif.解决Glide加载Gif非常慢问题
    电脑卡,eclipse Android stadio 卡,什么都卡解决方法
    Service IntentService区别 (面试)
    枚举
    Android stadio litepal
    Android 单元测试
  • 原文地址:https://www.cnblogs.com/xiaxinggege/p/2565423.html
Copyright © 2011-2022 走看看