附件下载功能实现

前端页面代码配置:

<form  action="" method="post" id="download2" style="display: none;">
    <input name="filename" value="" >
    <input type="submit" id="download3">
</form>

js 代码配置:

        需要你自己写下载调用 downloadTem() 这个方法。

function downloadTem(pathData){
    var url = window.location.href;
    var urlString = url.split("?");
    $('#download2').attr("action", "/trafficPermitAppendix/downloadFile?filePath=" + encodeURI(pathData));
    $('#download3').click();

}

controller 层代码配置:

        这种写法是后台直接在前台下载文件,不需要其他前台的操作。

@PostMapping("/downloadFile")
public void downloadFile(@RequestParam(name = "filePath") String filePathParam,HttpServletResponse resp) throws Exception {
		//解码操作
        String filePath = URLDecoder.decode(filePathParam, "UTF-8");
        String[] files = filePath.split("/");
        String directorys = filePath.substring(0, filePath.lastIndexOf("/"));
        String fileName = files[files.length-1];
        DataInputStream in = null;
        OutputStream out = null;
        resp.reset();
 
        if (fileName != null) {
            try {
                //获取项目的绝对路径
                String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
                String path2 = path.substring(1, path.length());
                String[] path3 = path2.split("/");
                String resultFileName = path2 + File.separator + "static" + File.separator + directorys + File.separator + fileName;
                InputStream inputStreamParam = new FileInputStream(resultFileName);
                resultFileName = URLEncoder.encode(resultFileName, "UTF-8");
                resp.setCharacterEncoding("UTF-8");
                resp.setHeader("Content-disposition", "attachment;filename=" + fileName);
                resp.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                in = new DataInputStream(inputStreamParam);
                out = resp.getOutputStream();
                int bytes = 0;
                byte[] bufferOut = new byte[1024];
                while ((bytes = in.read(bufferOut)) != -1) {
                    out.write(bufferOut, 0, bytes);
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("错误路径", e);
                resp.reset();//清空输出流
                resp.setContentType("text/html;charset=UTF-8");
                resp.getWriter().print("<script type='text/javascript'>layer.alert('文件不存在')</script>");
                resp.getWriter().close();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                        logger.error("io错误", ioe);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException ioe2) {
                        ioe2.printStackTrace();
                        logger.error("io错误", ioe2);
                    }
                }
            }
        }
	}

猜你喜欢

转载自blog.csdn.net/xhf852963/article/details/122239839