zoukankan      html  css  js  c++  java
  • 网页文件下载

    前台 jsp 
    function downVideo(value,row,index){
     return '<a href="<%=basePath%>admin/video/video!fileDown.ds?uname='+row.uname+'&filepath='+value+'">下载</a>';
    }
    后台java :
    action:
     public void fileDown() {
       FileUtil.download(filepath, getRequest().getParameter("uname")+filepath.substring(filepath.lastIndexOf("."),filepath.length()), getResponse());
      }
    FileUtil:
    public static void download(String filepath, String filename, HttpServletResponse response) {
            response.setContentType("application/x-download;charset=utf-8");
            OutputStream outp = null;
            FileInputStream in = null;
            try {
                response.addHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("gbk"),"ISO-8859-1"));
             
                in = new FileInputStream(ServletActionContext.getServletContext().getRealPath("/")+filepath);
                outp = response.getOutputStream();
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            byte[] b = new byte[1024];
            int i = 0;
            try {
                while ((i = in.read(b, 0, 1024)) != -1) {
                    outp.write(b, 0, i);
                    outp.flush();
                }
             
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    in = null;
                }
                if (outp != null) {
                    outp = null;
                }
            }
        }
     
  • 相关阅读:
    广大数据结构复习之顺序表
    vs2017编译器编写c语言代码函数安全问题
    大数据学习之Spark Streaming进阶 52
    大数据学习之Spark Streaming基础 52
    大数据学习之SparkSQL数据源 51
    本地eclipse idea 写的Hadoop的API和 spark程序不能访问云服务器中的数据
    大数据学习之SparkSQL 50
    【PHP】上传图片翻转问题
    【Linux】CentOS7 安装gdb
    【Linux】yum [Errno 14] HTTP Error 404
  • 原文地址:https://www.cnblogs.com/loveweiwei/p/4135453.html
Copyright © 2011-2022 走看看