zoukankan      html  css  js  c++  java
  • java io 文件下载功能

    一.

    @RequestMapping(value = "/download/{filename}")

    public void downloadFile(HttpServletRequest request,HttpServletResponse response,

    @PathVariable(value = "filename") String filename)

    throws IOException {

    //获取项目路径

    String readPath = request.getServletContext().getRealPath("/");

    readPath = readPath.substring(0, readPath.lastIndexOf("\"));

    readPath = readPath.substring(0, readPath.lastIndexOf("\"));

    //文件绝对路径

    String picPath = readPath+File.separator+"pai"+File.separator+"attachmentSimple"+File.separator+"1"+File.separator + filename;

    File file = new File(picPath);

    if(!file.exists()){

    throw new RuntimeException("视频文件不存在!");

    }

    response.setContentType("application/force-download");// 设置强制下载不打开

    response.addHeader("Content-Disposition",

    "attachment;fileName=" + filename);// 设置文件名

    FileInputStream inputStream = new FileInputStream(file);

    ServletOutputStream out = response.getOutputStream();

    int b = 0;

    byte[] buffer = new byte[1024];

    while (b != -1){

    out.write(buffer,0,b);

    b = inputStream.read(buffer);

    }

    inputStream.close();

    out.close();

    out.flush();

    }

    }

    二.

    @RequestMapping("download")

    public void download(@RequestParam("id") String id,

    HttpServletRequest request, HttpServletResponse response){

    VideoInfo video = (VideoInfo) videoService.getObjectById(VideoInfo.class, id);

    String path = request.getServletContext().getRealPath(video.getRestorePath());

    File file = new File(path);

    if(!file.exists()){

    throw new RuntimeException("视频文件不存在!");

    }

    String fileName = video.getRestorePath().substring(video.getRestorePath().indexOf(File.separator)+1);

    response.setContentType("application/force-download");// 设置强制下载不打开

    response.addHeader("Content-Disposition",

    "attachment;fileName=" + fileName);// 设置文件名

    byte[] buffer = new byte[1024];

    FileInputStream fis = null;

    BufferedInputStream bis = null;

    try {

    fis = new FileInputStream(file);

    bis = new BufferedInputStream(fis);

    OutputStream os = response.getOutputStream();

    int i = bis.read(buffer);

    while (i != -1) {

    os.write(buffer, 0, i);

    i = bis.read(buffer);

    }

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    if (bis != null) {

    try {

    bis.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    if (fis != null) {

    try {

    fis.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    }

  • 相关阅读:
    18.5 推挽输出和开漏输出区别
    19.3 Table 1-2.S3C2440A 289-Pin FBGA Pin Assignments (Sheet 4 of 9) (Continued)
    19.2 MEMORY CONTROLLER
    19.1 PORT CONTROL DESCRIPTIONS
    17.2 SourceInsight批量注释
    17.3 删除没用的project
    17.1 添加汇编文件并可索引
    16.2 在SecureCRT编写C程序不高亮显示
    16.1 解决SecureCRT的Home+End+Del不好用使用方法
    15.1 打开文件时的提示(不是dos格式)去掉头文件
  • 原文地址:https://www.cnblogs.com/zhi-ming/p/10453161.html
Copyright © 2011-2022 走看看