zoukankan      html  css  js  c++  java
  • spring boot — InputStream

    @Component
    public class TextFileDownloadView extends AbstractFileDownloadView {

    @Override
    protected InputStream getInputStream(Map<String, Object> model,
    HttpServletRequest request) throws IOException {
    Resource resource = new ClassPathResource("abc.txt");
    return resource.getInputStream();
    }

    @Override
    protected void addResponseHeader(Map<String, Object> model,
    HttpServletRequest request, HttpServletResponse response) {
    response.setHeader("Content-Disposition", "attachment; filename=abc.txt");
    response.setContentType("text/plain");

    }
    }

    @RequestMapping(value = "/downloadTxt", method = RequestMethod.GET)
    public String downloadTxt1() {
    return "textFileDownloadView";
    }

    Originate from http://rensanning.iteye.com/blog/2356942

    package inputstream;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import inputstream.domain.Customer;
    
    
    @RestController
    public class InputStreamController {
        
         private final Logger logger = LoggerFactory.getLogger(this.getClass()); 
         
        @RequestMapping(value = "" , method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE )
        public InputStream testClassPath() throws IOException {  
              Resource resource = new ClassPathResource("test.txt");  
              String fileName = resource.getFilename();  
              logger.info(fileName);
    //        resource.getFile();   //获取资源对应的文件  
    //        resource.getURL(); //获取资源对应的URL  
                 //每次都会打开一个新的流  
                 InputStream is = resource.getInputStream();  
                 //this.printContent(is);  
                 logger.info(is.toString());
                 return is;
           }      
        
        @RequestMapping(value = "/cs" , method = RequestMethod.GET , produces = MediaType.APPLICATION_JSON_VALUE )
        public Customer testCustomer() {
            Customer c = new Customer(5,"Tom");
            return c;
            
        }
    
    }

     http://www.cnblogs.com/wangtj-19/p/5889056.html

  • 相关阅读:
    vmware ubuntu 异常关机无法连接到网络
    Speed up GCC link
    常用的一些解压命令
    Log4j 漏洞复现
    Test Case Design method Boundary value analysis and Equivalence partitioning
    CCA (Citrix Certified Administrator) exam of “Implementing Citrix XenDesktop 4”
    What is Key Word driven Testing?
    SAP AGS面试小结
    腾讯2013终端实习生一面
    指针的引用
  • 原文地址:https://www.cnblogs.com/luffystory/p/7475963.html
Copyright © 2011-2022 走看看