IO —— 下载模板java

一、业务前提

下载模板,然后填写一些乘机人信息,再上传,后台解析数据,返回给前端页面,将数据拼接到表格中。
IO —— 下载模板java
IO —— 下载模板java

二、文件存放位置

IO —— 下载模板java

三、JSP代码

<div id="psgDiv" class="easyui-dialog" data-options="modal:true,closed:true" style="width:600px;height:320px">
			<p style="margin-top: 20px;">
				<form id="file_form" method="post" enctype="multipart/form-data">
					<div>
                         <span id="uploadfile_dl"></span>
                        <input type="file" name="fileField" id="tripTemp_file_id" onchange="uploadFile('tripTemp_file_id', 'file_form')" style="visibility:hidden;width:20px" />
                        <a class="caissa-btn10" onclick="selectFile()">上传文件</a>
                        <a href="javascript:void(0)" onclick="outPsgExcel()" >下载模板</a>
					</div>
				</form>
				
			</p>
			</br>
			<p style="margin-left: 25px;">
            	说明:下载模板按格式录入信息后上传文件,支持上传.xls、.xlsx格式
            </p>
		</div>

四、下载功能的JS代码

function outPsgExcel(){
    	   var url = contextPath+"/common/excelOut.do?permission_Key="+permission_Key;  //路径自己拼 和我不一定一样
    	    url = encodeURI(url);
    	    location.href = url;  
       }

五、下载功能的controller代码

@RequestMapping( value = "/excelOut.do")
    public void excelStandardTemplateOut(HttpServletRequest request,
           HttpServletResponse response) throws IOException{
		logger.info("下载乘机人模板开始");
        /*URL save = Thread.currentThread().getContextClassLoader().getResource("");
        String str =     save.toString();
        str=str.substring(6,str.length());
        str=str.replaceAll("%20", " ");  
        int num = str.lastIndexOf("caissatickets-web");//caissatickets-web为项目名,应用到不同的项目中,这个需要修改!
        str=str.substring(0, num+"caissatickets-web".length());*/ 找文件路径的方式一
		String str = new ClassPathResource("/").getFile().getParentFile().getParent().toString().replace("\\", "/") + "/pdf/";//找文件路径的方式二
        str = str +"psg.xlsx";//Excel模板所在的路径。
        logger.info("下载乘机人模板:"+str);
        File f = new File(str);
         // 设置response参数,可以打开下载页面
       response.reset();
       response.setContentType("application/vnd.ms-excel;charset=utf-8");
       try {
    	//下载文件的名称
           response.setHeader("Content-Disposition", "attachment;filename="+ new String(("导入乘机人模板" + ".xlsx").getBytes(), "iso-8859-1"));
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }
       ServletOutputStream out = response.getOutputStream();
       BufferedInputStream bis = null;
       BufferedOutputStream bos = null;
       try {
           bis = new BufferedInputStream(new FileInputStream(f));
           bos = new BufferedOutputStream(out);
           byte[] buff = new byte[2048];
           int bytesRead;
           while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
               bos.write(buff, 0, bytesRead);
           }
       } catch (final IOException e) {
           throw e;
       } finally {
           if (bis != null)
               bis.close();
           if (bos != null)
               bos.close();
       }
	}

六 导入文件

https://blog.****.net/han_xiaoxue/article/details/88823209