获取文件路径、常用

web项目文件获取路径:

  1. controller 里获取
private String realpath = this.getRequest().getSession().getServletContext().getRealPath("/rsa");
  1. 一般类获取 里获取
public static String thisfile = Thread.currentThread().getContextClassLoader().getResource("").getPath().replace("%20", " ");  //配置文件路径
    //截取下
thisfile.substring(0, thisfile.indexOf("WEB-INF/classes"))
getRequest().getRealPath("Excel_Model")+ "/dealmb.xls";// 路径 liux可用

//excel模板下载

public void plInsertMuban() {
            OutputStream os = null;
            try {
                String filenames = "批量导入商户信息.xls";  //excel名字
                String expenditurepath = getRequest().getRealPath("Excel_Model")+ "/dealmb.xls";// 路径
                FileInputStream  fs  = new  FileInputStream(expenditurepath); //模板
                HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(fs));//创建Excel文档
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename="+ new String(filenames.getBytes("gbk"), "iso8859-1"));
                os = response.getOutputStream();
                workbook.write(os);
                os.flush();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_25384901/article/details/78845757