String http输入输出流导入导出
导出(下载)
1 public static void exportString(String downFileName,String exportString , HttpServletResponse response,HttpServletRequest request) throws Exception{ 2 ByteArrayInputStream byteIs = new ByteArrayInputStream(exportString.getBytes("UTF-8")); 3 4 response.reset(); // 必要地清除response中的缓存信息 5 response.setHeader("Content-Disposition", "attachment; filename=" + new String(downFileName.getBytes("GBK"),"ISO8859-1")); 6 response.setContentType("application/text/html");//根据个人需要,这个是下载文件的类型 7 javax.servlet.ServletOutputStream out = response.getOutputStream(); 8 byte[] content = new byte[1024]; 9 int length = 0; 10 while ((length = byteIs.read(content)) != -1) { 11 out.write(content, 0, length); 12 } 13 out.flush(); 14 out.close(); 15 byteIs.close(); 16 }
导入
1 public void importTemplateDetail() throws Exception{ 2 if (file != null && file.isFile() && file.canRead() ) { 3 String result = ""; 4 InputStream input = null; //excel输入流 5 BufferedReader bufferedReader = null; 6 try { 7 input = new FileInputStream(file); 8 InputStreamReader read = new InputStreamReader(input); 9 bufferedReader = new BufferedReader(read); 10 String lineTxt = null; 11 String content = ""; 12 while((lineTxt = bufferedReader.readLine()) != null){ 13 content += lineTxt.trim(); 14 15 } 16 17 //密文长度,16进制 18 String encryptLength_16 = content.substring(0, 2); 19 Integer encryptLen = Integer.parseInt(encryptLength_16, 16); 20 //密文 21 String encrypt = content.substring(2, encryptLen+2); 22 //正文 23 String importContent = content.substring(encryptLen+2,content.length()); 24 //对正文长度加密并与密文对比,是否长度一致,不一致则表示与导出时的内容有异 25 String encryptionContentLength = EncryptionHelper.getInstance().encryptionToMD5(String.valueOf(importContent.length())); 26 if (!encrypt.equals(encryptionContentLength)) { 27 response("{\"success\":false,\"message\":\"导入内容有误,请核对后重新导入\"}"); 28 return; 29 } 30 31 System.out.println("16进制密文长度:"+encryptLen); 32 System.out.println("16进制密文:"+encrypt); 33 System.out.println("导入内容:"+importContent); 34 35 }catch (Exception e) { 36 System.out.println(e.getMessage()); 37 response("{\"success\":false,\"message\":\""+e.getMessage()+"\"}"); 38 return; 39 }finally{ 40 if(input!=null){ 41 input.close(); 42 } 43 if (bufferedReader!=null) { 44 bufferedReader.close(); 45 } 46 } 47 response("{\"success\":true,\"message\":\"导入成功!\"}"); 48 }else{ 49 response("{\"success\":false,\"message\":'文件读取失败!'}"); 50 } 51 }