• 图画(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;
              }
          }
    %>


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

  • 相关阅读:
    转战博客园
    C++虐恋:MBCS安装失败导致的四天误工
    Servlet 3.0 新特性详解 (转载)
    数据库连接池proxool的两种使用方式
    java异常处理中的细节
    InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute
    如果我是一个全栈极客,那么,下一步该怎么走?
    C++基础与提高 001
    用户及文件权限管理
    命令行操作体验
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4907578.html
Copyright © 2020-2023  润新知