zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    将一个Vue项目跑起来
    python2编码问题'ascii' codec can't encode character解决办法
    python实现normal equation进行一元、多元线性回归
    记一次安装CPU版本的TensorFlow(numpy出错,ddl出错)解决
    机器学习实战学习笔记(二)-KNN算法(2)-使用KNN算法进行手写数字的识别
    2019年年终总结(流水账)
    机器学习实战学习笔记(二)-KNN算法(2)-KNN算法改进约会网站的配对效果
    将博客搬至CSDN
    机器学习实战学习笔记(二)-KNN算法(1)-KNN的最基本实现
    机器学习实战阅读笔记(一)-机器学习基本概念
  • 原文地址:https://www.cnblogs.com/yadongliang/p/13209668.html
Copyright © 2011-2022 走看看