zoukankan      html  css  js  c++  java
  • springboot 读取 resource文件

    文件位置信息如图;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URLEncoder;
    import java.nio.charset.StandardCharsets;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.io.IOUtils;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.ResourceLoader;
    import org.springframework.util.LinkedMultiValueMap;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.gdtopway.common.utils.AjaxJson;
    import com.szsj.entity.sample.SampleGrid;
    import com.szsj.entity.sample.SampleInformatization;
    import com.szsj.entity.sample.SampleMarkettechnilogy;
    import com.szsj.entity.sample.SampleOthertechnology;
    import com.szsj.entity.sample.SampleProductiontechnology;
    import com.szsj.entity.sample.SampleScience;
    import com.szsj.entity.sample.SampleSmallconstruction;
    import com.szsj.entity.sample.SampleUtil;
    import com.szsj.entity.semantic.TreeVO;
    
    
    @RestController
    @RequestMapping("/sample")
    public class SampleController {
    
    
    	@javax.annotation.Resource
    	private ResourceLoader resourceLoader;
            
      /**
    	 * 下载抽样模板文件
    	 */
    	@RequestMapping("downLoadExcel")
    	public void downLoadExcel(HttpServletResponse response, HttpServletRequest request) {
    		InputStream inputStream = null;
        	ServletOutputStream servletOutputStream = null;
        	try {
        		String filename = "抽样模板.xls";
    	        String path = "cymb.xls";
    	        Resource resource = resourceLoader.getResource("classpath:"+path);response.setContentType("application/vnd.ms-excel");
    	        response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    	        response.addHeader("charset", "utf-8");
    	        response.addHeader("Pragma", "no-cache");
    	        String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
    	        response.setHeader("Content-Disposition", "attachment; filename="" + encodeName + ""; filename*=utf-8''" + encodeName);
    	        
    	        inputStream = resource.getInputStream();
    	        servletOutputStream = response.getOutputStream();
    	        IOUtils.copy(inputStream, servletOutputStream);
    	        response.flushBuffer();
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (servletOutputStream != null) {
    					servletOutputStream.close();
    					servletOutputStream = null;
    				}
    				if (inputStream != null) {
    					inputStream.close();
    					inputStream = null;
    				}
    				// 召唤jvm的垃圾回收器
    				System.gc();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    

      

  • 相关阅读:
    重构技巧 引入Null对象
    python yield
    todo
    Python 函数式编程学习
    Python 修饰器
    socket
    Exception、RuntimeException
    设计模式
    线程池
    VMware异常关闭后再次启动提示“以独占方式锁定此配置文件失败”!!!
  • 原文地址:https://www.cnblogs.com/leirenyuan/p/9811260.html
Copyright © 2011-2022 走看看