protected void outputFile(HttpServletResponse response, String parth, String fileName, byte[] file) throws UnsupportedEncodingException { if (ArrayUtils.isEmpty(file)) { logger.info("get file download url failed."); return; } // 设置相关头信息 response.setContentType("multipart/form-data"); fileName = StringUtils.isNotEmpty(fileName) ? fileName : parth.substring(parth.lastIndexOf("/")); // fileName = new String(fileName.getBytes("iso-8859-1"), Constants.UTF8); // 文件名编码 try { fileName = URLEncoder.encode(fileName, Constants.UTF8); } catch (UnsupportedEncodingException e) { logger.error("fileName encode failed. errorMessage[{}]", e.getMessage()); } response.setHeader("Content-disposition", "attachment;filename=" + fileName); response.addHeader("Pragma", "no-cache"); response.addHeader("Cache-Control", "no-cache"); response.addDateHeader("Expries", 0); // 返回文件 OutputStream out = null; try { out = response.getOutputStream(); out.write(file); out.flush(); } catch (IOException e) { logger.error("download file failed. errorMessage[{}]", e.getMessage()); } finally { if (null != out) { try { out.close(); } catch (IOException e) { logger.error("OutputStream close failed. errorMessage[{}]", e.getMessage()); } } } }