/** * 文件下载 * * @param request * @param response * @param id * @throws Exception * @throws NumberFormatException */ private void downLoadFile(HttpServletRequest request, String id) throws NumberFormatException, Exception { Contracttemplate ct = contracttemplateService .queryContracttemplateById(Integer.parseInt(id)); if (ct != null) { byte[] blobSource = ct.getBlobSource(); ByteArrayInputStream bais = new ByteArrayInputStream(blobSource); String uploadDir = request.getSession().getServletContext() .getRealPath("/") + "\\WEB-INF\\static\\contract\\"; File fileDir = new File(uploadDir); if (!fileDir.exists()) { fileDir.mkdirs(); //创建目录 } String uploadDirPath = request.getSession().getServletContext() .getRealPath("/") + "\\WEB-INF\\static\\contract\\" + "contracttemplate.doc"; File file = new File(uploadDirPath); if (!file.exists()) { file.createNewFile(); //创建文件 } FileOutputStream outputStream = new FileOutputStream(file); int size = 0; while ((size = bais.read(blobSource)) != -1) { outputStream.write(blobSource, 0, size); } if (outputStream != null) { outputStream.close(); } } }