zoukankan      html  css  js  c++  java
  • Java Web实现使用浏览器从服务器下载文件(后台)

    Java Web实现 使用浏览器从服务器下载文件。
    下面实现两种情况的下载,需求如下:

    需求(一):

    1、用户在页面填写表单。2、填写完成后,选择下载,将表单内容发往后台。
    3、后台根据内容生产一个文件,发送给前端。
    4、前端成功下载文件到本地。
    此需求简单来说就是,用户在页面上填写内容,然后将内容转变成文件的形式。

    后台设计思路:

    1、首先拿到前端发送过来的内容。
    2、将内容解析,存放至缓冲区。
    3、设置响应头。
    4、将缓冲区里的内容,以流的方式写出。

    代码实现:

     public void download(HttpServletResponse response){
            JSONObject json = new JSONObject();
            /* 假设这是前端使用JSON放过来的数据 */
            json.put("A","1");
            json.put("B","2");
            json.put("C","3");
            /* 遍历JSON,将JSON的值放至缓冲区 */
            StringBuilder sb = new StringBuilder();
            Iterator it = json.keys();
            while(it.hasNext()){
                String key =  (String)it.next();
                String value = (String)json.get(key);
                sb.append(key).append("=").append(value).append("
    ");
            }
            /*  设置文件ContentType类型,这样设置,会自动判断下载文件类型   */
            response.setContentType("application/multipart/form-data");
            /* 设置文件头:最后一个参数是设置下载文件名(假如我们叫a.ini)   */
            response.setHeader("Content-Disposition", "attachment;filename=a.ini");
            try{
                /* 用流将数据写给前端 */
                OutputStream os = response.getOutputStream();
                os.write(sb.toString().getBytes());
                os.flush();
                os.close();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
        }

     

    需求(二):

    1、用户指定下载什么文件。
    2、成功下载文件到本地。

    后台设计思路:

    1、首先获取前端需要下载什么文件。
    2、找到文件在服务器上面的路径。
    3、将文件读进内存
    4、将文件以流的方式写出。

    代码实现:

        public void download() throws  IOException{
            //获取服务器文件
            File file = new File("/home/a.txt");
    
            InputStream ins = new FileInputStream(file);
            /* 设置文件ContentType类型,这样设置,会自动判断下载文件类型 */
            response.setContentType("multipart/form-data");
            /* 设置文件头:最后一个参数是设置下载文件名 */
            response.setHeader("Content-Disposition", "attachment;filename="+file.getName());
            try{
                OutputStream os = response.getOutputStream();
                byte[] b = new byte[1024];
                int len;
                while((len = ins.read(b)) > 0){
                    os.write(b,0,len);
                }
                os.flush();
                os.close();
                ins.close();
            }catch (IOException ioe){
               ioe.printStackTrace();
            }
        }

    原文地址:https://blog.csdn.net/qq_28082757/article/details/72771166

  • 相关阅读:
    c#自动更新+安装程序的制作
    VS2013项目受源代码管理向源代码管理注册此项目时出错
    WinDbg配置和使用基础
    InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
    PowerDesigner 如何生成数据库更新脚本
    用户故事(User Story)
    Troubleshooting Record and Playback issues in Coded UI Test
    Coded UI
    compare two oracle database schemas
    How to: Use Schema Compare to Compare Different Database Definitions
  • 原文地址:https://www.cnblogs.com/zagwk/p/14824614.html
Copyright © 2011-2022 走看看