• Spring mvc 下载文件处理


    @RequestMapping(value = "downFile")
    public void downFile(HttpServletResponse response, String name,
    HttpServletRequest request) {
    ServletContext sc = request.getSession().getServletContext();
    String url = sc.getRealPath("/upload/" + name);
    File file = new File(url);

    //以下两种文件的下载流的处理方式,第二个方法感觉比较好
    downFileWidthData(response, name, url, file);// 用

    // downFileWidthBuffer(response, name, file);//运用buffer

    }

    /**
    * @param response
    * @param name
    * @param file
    */
    private void downFileWidthBuffer(HttpServletResponse response, String name,
    File file) {
    Date date = new Date();
    long start = System.currentTimeMillis();
    System.out.println(start);
    BufferedOutputStream bos = null;
    FileInputStream fis = null;
    try {

    response.addHeader("Content-Length", "" + file.length());
    response.addHeader("Content-Disposition", "attachment;filename="
    + new String(name.getBytes("gbk"), "iso-8859-1"));

    response.setContentType("application/octet-stream;charset=UTF-8");
    response.setContentType("application/octet-stream;charset=UTF-8");

    } catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    try {
    fis = new FileInputStream(file);
    byte[] buffer = new byte[fis.available()];
    fis.read(buffer);

    bos = new BufferedOutputStream(response.getOutputStream());

    bos.write(buffer);
    bos.flush();

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {

    try {
    if (fis != null) {
    fis.close();
    }
    if (bos != null) {
    bos.close();

    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    long end = System.currentTimeMillis();

    System.out.println(end - start);
    }

    /**
    * @param response
    * @param name
    * @param url
    * @param file
    */
    private void downFileWidthData(HttpServletResponse response, String name,
    String url, File file) {
    long start = System.currentTimeMillis();
    System.out.println(start);
    FileInputStream fis = null;
    try {
    fis = new FileInputStream(file);
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    DataOutputStream dos = null;
    OutputStream os = null;
    try {
    url = URLEncoder.encode(url, "utf-8");
    // response.addHeader("Context-Disposion",
    // "Attachment:filename="+URLEncoder.encode(name, "utf-8"));
    response.addHeader("Content-Length", "" + file.length());
    response.addHeader("Content-Disposition", "attachment;filename="
    + new String(name.getBytes("gbk"), "iso-8859-1"));
    response.setContentType("application/octet-stream;charset=UTF-8");
    os = response.getOutputStream();
    dos = new DataOutputStream(os);
    byte[] b = new byte[1024];
    int len;
    while ((len = fis.read(b)) != -1) {
    dos.write(b, 0, len);
    }
    dos.flush();
    os.flush();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {

    try {
    if (os != null) {
    os.close();
    }
    if (dos != null) {
    dos.close();
    }
    if (fis != null) {

    fis.close();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);

    }

  • 相关阅读:
    第十周博客总结
    校验码
    python-第五章习题
    python 课后习题 猜数游戏
    python 数据分析师
    python opencv图像的均值滤波、中值滤波和高斯滤波
    RPC原理及RPC实例分析
    eclipse Android创建相对布局和线性布局,主界面跳转
    python 安装opencv库的方法及图像边缘检测例子
    第三周博客总结
  • 原文地址:https://www.cnblogs.com/guoke-jsp/p/4339616.html
Copyright © 2020-2023  润新知