//上传--
// 1 保存数据到excel,对应的sheet头header,body数据类型list
//2 上传文件到服务器中
//下载--
// 1 获取文件路径
//2 导出数据到保持的路径(或者是浏览器路径)
/**
* 保存数据到excel
* @param listBody
* @param sheetName
*/
public void saveExcelData(List<DocLibBrowseOrgVo> listBody, String sheetName) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(sheetName);
XSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("");
row.createCell(1).setCellValue("");
row.createCell(2).setCellValue("");
int i = 0;
for (DocLibBrowseOrgVo doc : listBody) {
i++;
row = sheet.createRow(i);
row.createCell(0).setCellValue(doc.getDocLibId());
row.createCell(1).setCellValue(doc.getDocLibName());
row.createCell(2).setCellValue(doc.getCount());
row.createCell(4).setCellValue(doc.getStatisticDate());
}
}
/**
* 上传文件到服务器指定路径
*
* @param destPath
* @param fileName
*/
public void uploadFile(String destPath, String fileName, XSSFWorkbook wb) throws IOException {
File pathFile = new File(destPath);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
File destFile = new File(destPath + fileName);
BufferedOutputStream buffer = new BufferedOutputStream(new FileOutputStream(destFile));
wb.write(buffer);
buffer.close();
}
/**
* 下载
*
* @param srcPath
* @param fileName
*/
public void downLoadFile(String srcPath, String fileName, HttpServletRequest request, HttpServletResponse response) {
File file = new File(srcPath + fileName);
if (file.exists()) {
response.setContentType("application/force-download");// 设置强制下载不打开
setFileDownloadHeader(request, response, fileName); //解决中文名称
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
os = response.getOutputStream();//到浏览器下载路径
int i;
while ((i=bis.read(buffer))!= -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 解决中文名称
* @param request
* @param response
* @param fileName
*/
public void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response, String fileName) {
try {
//中文文件名支持
String encodedfileName;
String agent = request.getHeader("USER-AGENT");
if (null != agent && agent.contains("MSIE")) {//IE
encodedfileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else if (null != agent && agent.contains("Mozilla")) {
encodedfileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
} else {
encodedfileName = java.net.URLEncoder.encode(fileName, "UTF-8");
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}