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

  • 相关阅读:
    c#中将IP地址转换成无符号整形数的方法与逆变换方法
    中英文字符的截取
    c#读取并分析sql Server2005数据库日志
    枚举enum使用
    LINQ to XML 编程基础
    c#异步调用的几种方式
    File,FileInfo,FileStream,StreamReader的区别与用法
    Page.ClientScript.RegisterStartupScript用法小结
    asp.net图片上传及删除
    FileUpload控件
  • 原文地址:https://www.cnblogs.com/luffystory/p/7475963.html
Copyright © 2011-2022 走看看