• java文件下载


    文件下载

    大家都知道在web页面上如果一个有个连接,连接的的是文本文件,当左键点击的话会查看这个文件,右键点击可以下载.但是如果是windows不识别的文件,左键点击直接就下载了比如zip,那如何点击左键直接下载.txt的文本文件呢.请帮助...

     jsp方式

        <meta http-equiv="Content-Type" content="text/html; charset=gbk">  
        <HTML>  
        <HEAD> 
        </HEAD>
        <BODY>  
             <a href = "download1.jsp?filepath=d:\&filename=1a.txt" >downloadtest1</a>  
        </BODY>  
        </HTML>  

    文件在本地服务器(download.jsp)

        <%   
            String filename = request.getParameter("filename");//"1a.txt";   
            String filepath = request.getParameter("filepath");//"d:\";  
             int i = 0;  
            response.setContentType("application/octet-stream");  
            response.setHeader("Content-Disposition","attachment;filename = "+filename);   
            java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath+filename);  
            while((i= fileInputStream.read()) != -1){  
                out.write(i);  
            }  
        %>  

    文件在远程服务器(download.jsp)

    <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
    <%@ page import="java.util.*"%>
    <%@ page import="java.net.HttpURLConnection"%>
    <%@ page import="java.net.URL"%>
    <%@ page import="java.net.URLEncoder"%>
    <%@ page import="java.io.DataInputStream" %>
    <%@ page import="java.io.OutputStream" %>
    <%
        //远程文件名及路径
        String file = request.getParameter("file");
        //下载到本地的文件名
        String localName = file.substring( file.lastIndexOf("/")+1 );
        //解决中文文件名乱码
        localName = new String(localName.getBytes("gb2312"),"iso8859-1");
        
        response.reset() ;
        response.setContentType("application/force-download");    
           response.addHeader("Content-Disposition","attachment;filename="+localName  );     
           
           URL url = null;
        DataInputStream in = null;
        HttpURLConnection connection = null;
        OutputStream outputStream = null ;
        try{
            url = new URL(file);  
            connection = (HttpURLConnection) url.openConnection();  
            in = new DataInputStream(connection.getInputStream());
            outputStream =  response.getOutputStream();
           
            byte[] buffer = new byte[4096];  
            int count = 0;  
            
            while((count= in.read()) != -1){ 
                outputStream.write(count);     
            }  
        } catch (Exception e) {  
          e.printStackTrace();
          out.write("下载文件出错");
          out.write(e.getMessage());
        }finally{
            outputStream.flush();     
            if( in != null ) { in.close(); in = null;}
              out.clear();     
              out = pageContext.pushBody();    
            if( in != null ) { in.close(); in = null;}
            connection.disconnect();
            url = null;
        }  
     
    %>

    这是被调用的download1.jsp,这个jsp就是执行直接下载文件的不管是txt还是word文档都可以直接下载。

    js方式(可能会有安全方面的提示)

    function svcode() {
        var winname = window.open('D:/Project/cimissworkspace/GDS/WebContent/js/jquery.min.js', '_blank', 'height=1,width=1,top=200,left=300');
        winname.document.open('text/html', 'replace');
        winname.document.writeln(obj.value);
        winname.document.execCommand('saveas','','code.txt');
        winname.close();
    } 

    感谢

    https://blog.csdn.net/leolu007/article/details/7835729

  • 相关阅读:
    c++---------------------------->>>>>>>>>>>>>>>>>>遍历文件工具
    图像分割------>>>>>>性能提升30%以上!产业SOTA的实时实例分割算法SOLOv2,更快更强!
    不用绿幕也能实时抠图,商汤等提出只需单张图像、单个模型的新方法MODNet
    目标检测框回归问题
    NeurIPS 2020 | 微软亚洲研究院论文摘录之目标检测篇
    动态规划算法
    FCOS环境搭建配置
    conda--------------------------------->虚拟环境创建
    W: Failed to fetch http://ppa.launchpad.ne
    shell-code-5-函数
  • 原文地址:https://www.cnblogs.com/yadongliang/p/13209668.html
Copyright © 2020-2023  润新知