zoukankan      html  css  js  c++  java
  • 图画(txt等一下)实施开放的默认下载的默认浏览器,而不是(Java文本)

    在网络上,假设我们超链接地址对应于jpg档,txt档,点击链接,默认浏览器打开这些文件,而不是下载,那么,你如何实现竞争力的默认下载。

    1.可通过自己写一个download.jsp来实现

    <%@ page contentType="text/html; charset=UTF-8"%>
    <%@ page import="java.net.*"%>
    <%
    	//得到文件名称字和路径  
    	String filename = request.getParameter("filename");
    	String filepath = request.getParameter("filepath");
    	String displayfilename = URLEncoder.encode(filename,"UTF-8");
    	try {
    		response.setContentType("application/x-download");
    		response.setHeader("Content-Disposition","attachment;filename="" + displayfilename + """);
    		//HttpServletContext httpServletContext = (HttpServletContext)application;
    	    RequestDispatcher dis = application.getRequestDispatcher(filepath + filename);  
    	    //application.getRequestDispatcher(url)这里的url仅仅能是相对路径,如/upload/1.jpg
    	    if (dis != null) { 
    	        dis.forward(request,response);
    	    }
    	    response.flushBuffer();
    	} catch (Exception e) {  
    		e.printStackTrace();
    	    System.out.println("下载取消:" + filepath + filename);
    	} 
    	out.clear();
        out = pageContext.pushBody();
    %>

    当我们要链接图片(或其它浏览器默认打开的格式,这里以图片为例)时。把相应的文件名称和地址传入download.jsp的filename和filepath參数里。详细写法例如以下


    <a class="blue-line-a" href=" /PackTool/download.jsp?

    filename=WinGUI.exe&filepath=/other/WinGUI.exe">/other/WinGUI.exe</a>

    通过这样方式,我们就能实现文件的默认下载了,而不是浏览器的默认打开。

    2.通过文件输出流方式(推荐):

    <%@ page language="java" contentType="application/x-msdownload"    pageEncoding="UTF-8"%>
    <%@ page import="java.net.*"%>
    <%@ page import="java.io.*"%>
    <%@ page import="com.hikvision.modules.guide.util.*"%>
    <%
          //关于文件下载时採用文件流输出的方式处理:
          //加上response.reset(),而且全部的%>后面不要换行。包含最后一个;
    	  String filename = request.getParameter("filename");
    	  String filepath = request.getParameter("filepath");
          response.reset();//能够加也能够不加
          response.setContentType("application/x-download");
          //"想办法找到要提供下载的文件的物理路径+文件名称";D:/PackTools/shareFolder
          String filedownload = GetSharePathXml.getShareFolderPath() + filename;
          //"给用户提供的下载文件名称" 
          String filedisplay = filename;
          filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
          response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
    
          OutputStream outp = null;
          FileInputStream in = null;
          try
          {
              outp = response.getOutputStream();
              in = new FileInputStream(filedownload); 
    
              byte[] b = new byte[1024];
              int i = 0;
    
              while((i = in.read(b)) > 0)
              {
                  outp.write(b, 0, i);
              }
              outp.flush();
          }
          catch(Exception e)
          {
              System.out.println("Error!");
              e.printStackTrace();
          }
          finally
          {
              if(in != null)
              {
                  in.close();
                  in = null;
              }
              if(outp != null)
              {
                  outp.close();
                  out.clear();
                  outp = null;
              }
          }
    %>


    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    linuxepoll研究 Geek_Ma 博客园
    socklen_t 类型 blueliuyun的专栏 博客频道 CSDN.NET
    自己动手写web服务器一(浏览器的访问信息) 任天胜的个人空间 开源中国社区
    UNIX Domain Socket IPC blueliuyun的专栏 博客频道 CSDN.NET
    How to use epoll? A complete example in C
    gzip头部格式 任天胜的个人空间 开源中国社区
    CWnd与HWND的区别与转换
    MFC 框架各部分指针获取方式
    windows 注册表的编程
    VS2010创建C++项目类向导和智能感知不可用
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4907578.html
Copyright © 2011-2022 走看看