zoukankan      html  css  js  c++  java
  • java 通用文件下载 excel,pdf,word,jpg,exe,rar

    <%@ page contentType="text/html; charset=GBK" %>
      <%@ page autoFlush="false" %>
      <%@ page import="java.sql.*" %>
      <%@ page import="java.io.*" %>
      <%@ page import="java.util.*" %>
      <%@ page import="java.awt.*" %>
      <%@ page import="java.awt.image.*" %>
      <%@ page import="com.sun.image.codec.jpeg.*" %>
      <%@ page import="com.sun.image.codec.jpeg.*" %>

      <%!
        public static String getcontentType(String pextFileName) //获得扩展名 by gisoracle  {
          String pext=pextFileName.toLowerCase();
          if (pext.equals(".xls"))
          {
            return "application/vnd.ms-excel";
          }
          if (pext.equals(".doc"))
          {
            return "application/msword";
          }
          if (pext.equals(".txt"))
          {
            return "text/plain";
          }
          if (pext.equals(".pdf"))
          {
            return "application/pdf";
          }
          if (pext.equals(".jpg")||pext.equals(".jpeg"))
          {
            return "image/jpeg";
          }
          if (pext.equals(".ppt"))
          {
            return "application/vnd.ms-powerpoint";
          }
          if (pext.equals(".gif"))
          {
            return "image/gif";
          }
          return "text/html";
        }
    %>
    <%
      String filename =request.getParameter("filename");
      File downFile = new File(filename);
      String fileext=FileOper.getFiletype(downFile);
      System.out.println("fileext="+fileext);
      String contentType1 =getcontentType(fileext);
      System.out.println("contentType1="+contentType1);
      try {
      out.clear();
      response.setContentType(contentType1);
      filename = new String(filename.getBytes("GBK"), "ISO-8859-1");
      response.setHeader("Content-Disposition","attachment; filename=" +downFile.getName());
      response.addHeader("Cache-Control", "no-cache");
      InputStream blobStream = new FileInputStream(downFile);
      ServletOutputStream outStream = response.getOutputStream();
      byte[] buffer = new byte[10 * 1024];
      int nbytes = 0;
      while ( (nbytes = blobStream.read(buffer)) != -1) {
      outStream.write(buffer, 0, nbytes);
      }
      outStream.flush();
      outStream.close();
      blobStream.close();
      }
      catch (Exception e) {
      System.out.println(e);
      }
    %>

    -------------------------------------------------

    分装为方法调用:

    public void downFile() throws FileNotFoundException {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            // String filename =request.getParameter("filename");
            HttpSession session = (HttpSession) request.getSession();
            ServletContext application = (ServletContext) session
                    .getServletContext();
            String fileUrl = application.getRealPath("")
                    + "/wtVideo/video/file/WebComponents.exe";// 获取所需要下载文件的url
            String filename = fileUrl;
            File downFile = new File(filename);
            // String fileext=File.getFiletype(downFile);
            // System.out.println("fileext="+fileext);
            String contentType1 = this.getcontentType(filename);
            System.out.println("contentType1=" + contentType1);
            try {
                response.setContentType(contentType1);
                filename = new String(filename.getBytes("GBK"), "ISO-8859-1");
                response.setHeader("Content-Disposition", "attachment; filename="
                        + downFile.getName());
                response.addHeader("Cache-Control", "no-cache");
                InputStream blobStream = new FileInputStream(downFile);
                ServletOutputStream outStream = response.getOutputStream();
                byte[] buffer = new byte[10 * 1024];
                int nbytes = 0;
                while ((nbytes = blobStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, nbytes);
                }
                outStream.flush();
                outStream.close();
                blobStream.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }

  • 相关阅读:
    【小错误】ORA-00265: instance recovery required, cannot set ARCHIVELOG mode
    【小错误】Device eth2 has different MAC address than expected, ignoring.
    Bloom filters 布隆过滤器
    ORA-600 [729] "UGA Space Leak" (文档 ID 31056.1)
    Procwatcher: Script to Monitor and Examine Oracle DB and Clusterware Processes (文档 ID 459694.1)
    TECH: Getting a Stack Trace from a CORE file on Unix (文档 ID 1812.1)
    Diagnostic Tools Catalog (文档 ID 559339.1)
    How to Analyze Problems Related to Internal Errors (ORA-600) and Core Dumps (ORA-7445) using My Oracle Support (文档 ID 260459.1)
    windows DOS命令
    收集UNDO管理信息的脚本
  • 原文地址:https://www.cnblogs.com/yongwuqing/p/3968455.html
Copyright © 2011-2022 走看看