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


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值