java文件下载

2021-04-30 13:26

阅读:429

标签:context   except   attach   一个   byte   ext   ati   command   直接   

文件下载

大家都知道在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" >downloadtest1a>  
    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

 

java文件下载

标签:context   except   attach   一个   byte   ext   ati   command   直接   

原文地址:https://www.cnblogs.com/yadongliang/p/13209668.html


评论


亲,登录后才可以留言!