JAVA批量下载
来源:互联网 发布:红蜘蛛教学软件 编辑:程序博客网 时间:2024/05/29 15:15
1. 根据数据库数据批量生成文件。
2. 通过poi包生成excel
3. 通过word模版,替换参数,生成word
4. 将文件打包下载
5. workspace: UTF-8
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { try { Cred cred = validate(request); int confId = validateConfId(request); List<Object[]> exportPartsData = null; List<Object[]> exportUsersData = null; ConferenceDAO confDao = getConfById(confId); if(confDao != null) { exportPartsData = this.getExportParticipantData(request, cred, confDao); exportUsersData = this.getExportUsersData(request, cred, confDao); } //excel file String exportExcelFileName = this.getExportExcelFileName(confDao.getConfName(), confDao.getStartTime()); generateExcel(response, exportPartsData, exportUsersData, exportExcelFileName); //word file String exportWrodFileName = this.getExportWordFileName(confDao.getConfName(), confDao.getStartTime()); generateWord(response, confDto, exportWrodFileName); //zip file String fileName = getExportFileName(confDao.getConfName()); downloadZipFile(request, response, fileName, exportExcelFileName, exportWrodFileName); } catch (Throwable ex) { String msg = "Failed to export data from video system"; log.error(msg, ex); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to export data from video system."); return; } }
protected void generateExcel(HttpServletResponse response, List<Object[]> parts, List<Object[]> users, String fileName) throws IOException {
//import org.apache.poi.hssf.usermodel.HSSFCell;//import org.apache.poi.hssf.usermodel.HSSFRow;//import org.apache.poi.hssf.usermodel.HSSFSheet;//import org.apache.poi.hssf.usermodel.HSSFWorkbook;//import org.apache.poi.xwpf.usermodel.XWPFDocument; HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("会场参数"); if(parts != null && parts.size() > 0) { for(int i=0; i<parts.size(); i++ ) { HSSFRow row = sheet1.createRow(i); for(int j=0; j<7; j++) { HSSFCell cell = row.createCell(j); cell.setCellValue(parts.get(i)[j] == null ? "" : parts.get(i)[j].toString()); } } } HSSFSheet sheet2 = wb.createSheet("通讯录"); if(users != null && users.size() > 0) { for(int i=0; i<users.size(); i++ ) { HSSFRow row = sheet2.createRow(i); for(int j=0; j<5; j++) { HSSFCell cell = row.createCell(j); cell.setCellValue(users.get(i)[j] == null ? "" : users.get(i)[j].toString()); } } } FileOutputStream xls = new FileOutputStream(fileName); try { wb.write(xls); xls.flush(); } catch (Exception e) { String msg = "generate excel fail the fileName: " + fileName; log.error(msg, e); throw new RuntimeException(e); } finally { xls.close(); } }
protected void generateWord(HttpServletResponse response, ConferenceDTO confDto, String fileName) throws IOException { Map<String, Object> param = new HashMap<String, Object>(); param.put("${dept}", confDto.getDepartment() == null ? "" : confDto.getDepartment().getName()); param.put("${confName}", confDto.getConfName()); param.put("${startTime}", TimeUtil.format(confDto.getStartTime())); param.put("${speakParty}", confDto.getSpeakParty());
//................................................
if(confDto.isIncludePT()) { if(confDto.getInteractive() == 0) { param.put("${confMode1}", "有"); } else if(confDto.getInteractive() == 1) { param.put("${confMode3}", "有"); } else { param.put("${confMode1}", ""); } } XWPFDocument doc = null; OutputStream xls = null; try { doc = WordUtil.generateWord(param,"d:/technicalSupportNotice.docx"); xls = new FileOutputStream(fileName); doc.write(xls); xls.flush(); } catch (Exception e) { String msg = "generate word fail the fileName: " + fileName; log.error(msg, e); throw new RuntimeException(e); } finally { xls.close(); } }
private void downloadZipFile(HttpServletRequest request, HttpServletResponse response, String fileName, String fileName1, String fileName2) {
//import org.apache.tools.zip.ZipEntry; //import org.apache.tools.zip.ZipOutputStream; ZipOutputStream zos = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); zos = new ZipOutputStream(baos); zos.setEncoding("gbk");// very import Zip(fileName1, fileName2, zos); zos.closeEntry(); zos.flush(); zos.close(); byte[] tba = baos.toByteArray(); baos.flush(); baos.close(); response.reset(); response.setContentType(DEFAULT_CONTENT_TYPE); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); ServletOutputStream servletOS = response.getOutputStream(); servletOS.write(tba); servletOS.flush(); servletOS.close(); } catch (FileNotFoundException e) { throw new UnsupportedOperationException("FileNotFoundException Exception", e); } catch (IOException e) { throw new UnsupportedOperationException("IOException Exception", e); } catch (Exception e) { throw new UnsupportedOperationException("Exception", e); } } private void Zip(String fileName1, String fileName2, ZipOutputStream out) throws IOException { File file1 = new File(fileName1); File file2 = new File(fileName2); if(file1.exists()) { out.putNextEntry(new ZipEntry(file1.getName())); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1)); int readLine = 0; int bufferSize = 1024 * 8; byte[] buffer = new byte[bufferSize]; while((readLine = bis.read(buffer, 0 , bufferSize)) != -1) { out.write(buffer, 0, readLine); } bis.close(); } if(file2.exists()) { out.putNextEntry(new ZipEntry(file2.getName())); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file2)); int readLine = 0; int bufferSize = 1024 * 8; byte[] buffer = new byte[bufferSize]; while((readLine = bis.read(buffer, 0 , bufferSize)) != -1) { out.write(buffer, 0, readLine); } bis.close(); } //delete temp file if(file1.exists()) { file1.delete(); } if(file2.exists()) { file2.delete(); } }
0 0
- java批量下载
- java 批量下载文件
- java 批量下载文件
- java实现批量下载
- java批量下载
- java 批量下载文件
- java批量下载文件
- JAVA批量下载
- java打包批量下载
- Java批量下载zip文件
- JAVA上传批量下载文件
- 用Java批量下载PDF
- Java批量文件打包下载
- java 批量下载网络文件
- JAVA WEB 批量文件下载
- java 文件 批量下载 记录
- Java批量文件打包下载
- java-flex 批量下载文件-压缩下载
- 在高通平台添加或者移植一个完整的camera
- SpringMVC源码总结(二)mvc:annotation-driven以及@Controller和@RequestMapping的那些事
- 「视频直播技术详解」系列之二:处理
- linux目录树架构示意图
- 下载图片
- JAVA批量下载
- JavaScript笔记:对象
- spring mvc中的@propertysource
- linux下快捷启动关闭tomcat
- Scrapy下xpath基本的使用方法
- linux中的rpm -ivh
- 欢迎使用CSDN-markdown编辑器
- HDOJ 4699 Editor
- EtherCAT主站SOEM源码解析----同步管理器SM配置