zoukankan      html  css  js  c++  java
  • JAVA中实现根据文件路径下载文件

    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class TestUtil {
        /*
        根据文件所在路径下载文件
         */
        public void download(HttpServletResponse response, String filePath){
            File file = new File(filePath);
            // 取得文件名。
            String fileName = file.getName();
            InputStream fis = null;
            try {
                fis = new FileInputStream(file);
                response.reset();
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition",
                        "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));
                response.setHeader("Content-Length", String.valueOf(file.length()));
    
                byte[] b = new byte[1024];
                int len;
                while ((len = fis.read(b)) != -1) {
                    response.getOutputStream().write(b, 0, len);
                }
                response.flushBuffer();
                fis.close();
            }catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    

      如以上方法在不同浏览器下载文件名乱码,可换用以下方法:

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URLEncoder;
    
    public class TestUtil {
        /*
        根据文件所在路径下载文件
         */
        public void download(HttpServletRequest request, HttpServletResponse response, String filePath){
            File file = new File(filePath);
            // 取得文件名。
            String fileName = file.getName();
            InputStream fis = null;
            try {
                fis = new FileInputStream(file);
                request.setCharacterEncoding("UTF-8");
                String agent = request.getHeader("User-Agent").toUpperCase();
                if ((agent.indexOf("MSIE") > 0) || ((agent.indexOf("RV") != -1) && (agent.indexOf("FIREFOX") == -1)))
                    fileName = URLEncoder.encode(fileName, "UTF-8");
                else {
                    fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
                }
                response.reset();
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
                response.setHeader("Content-Length", String.valueOf(file.length()));
    
                byte[] b = new byte[1024];
                int len;
                while ((len = fis.read(b)) != -1) {
                    response.getOutputStream().write(b, 0, len);
                }
                response.flushBuffer();
                fis.close();
            }catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    

      

  • 相关阅读:
    【经验】AngularJS
    jquery复选框选择 DoTop
    SQL查询数据库名、表名、列名 DoTop
    C#读取配置文件中的信息 DoTop
    ASP.NET获取工程根目录的方法集合 DoTop
    ASP.NET前台Html.DropDownList的使用 DoTop
    JS的同步和异步加载
    tornado nginx 同源(AccessControlAllowOrigin)错误处理记录
    sql join 的一次小使用
    关于CSS3 animation 属性在ie edge浏览器中不能工作
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/10653147.html
Copyright © 2011-2022 走看看