zoukankan      html  css  js  c++  java
  • SpringBoot环境下java实现文件的下载

    思路:文件下载,就是给服务器上的文件创建输入流,客户端创建输出流,将文件读出,读入到客户端的输出流中,(流与流的转换)

    package com.cst.icode.controller;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.cst.util.file.Files;
    import com.cst.util.file.Files.CstDir;
    
    
    @RestController
    @RequestMapping("/file")
    public class TestController {
    	
    	@GetMapping("/froalar/get/{path}")
    	public Object getFroalarFile(@PathVariable String path,HttpServletResponse re) {
    		path=path.replace(":", File.separator);
    		// 平台的下载文件根路径
    		File uploadRoot = Files.getDir(CstDir.upload);//=后面是要获得下载的文件的路径
    		File downloadDir = new File(uploadRoot,  File.separator+path);
    		return downloadFile(downloadDir,re);
    	}
    	private OutputStream downloadFile(File file,HttpServletResponse re) {
    		if (file.exists()) {
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    int i = bis.read(buffer);
                    OutputStream os = re.getOutputStream();
                    while (i != -1) {
                    	os.write(buffer,0,i);
                        i = bis.read(buffer);
                    }
                    return os;
                } 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();
                        }
                    }
                }
            }
    		return null;
    	}
    
    }
    

      

  • 相关阅读:
    codeforces 617 E. XOR and Favorite Number(莫队算法)
    bzoj 2038 [2009国家集训队]小Z的袜子(hose) 莫队算法
    HDU 6024 Building Shops (简单dp)
    Codeforces Round #417 B. Sagheer, the Hausmeister
    第九届ECNU Coder K.计软联谊
    解决在linux安装网易云音乐无法点击图标打开
    [洛谷P1169] [ZJOI2007]棋盘制作
    [洛谷P2577] [ZJOI2005]午餐
    [洛谷P1941] 飞扬的小鸟
    [洛谷P1600] 天天爱跑步
  • 原文地址:https://www.cnblogs.com/chcha1/p/11313677.html
Copyright © 2011-2022 走看看