• 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();

    }

    }

    }

    }

  • 相关阅读:
    juc之ConcurrentHashMap在我工作中的实践
    设计模式在我工作中的实践
    SpringBoot突报java.lang.NoSuchFieldError分析
    JQuery.UI类库AutoComplete 调用WebService进行模糊查询
    DevExpress控件库----LookUpEdit控件 和 GridLookUpEdit控件使用
    DevExpress控件库----FlyoutPanel提示控件
    Android学习笔记---Log与Activity生命周期
    DevExpress控件库---MarqueeProgressBarControl控件和ProgressBarControl控件
    DevExpress控件库---TokenEdit控件使用
    DevExpress控件库----SearchLookUpEdit控件
  • 原文地址:https://www.cnblogs.com/zhi-ming/p/10453161.html
Copyright © 2020-2023  润新知