1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
/** The inputStream */
private InputStream inputStream;
public String exportToJsonFile()
throws JsonMappingException, JsonGenerationException, IOException
{
try {
Map<String, String> objectMap = new HashMap<String, String>();
if(isRoleBeanChecked() != null){
if(isRoleBeanChecked()){
Collection<RoleBean> roleList = this.getRoleBeanService().getListRoleBean();
String tmp = this.getRoleBeanService().exportIntoJson(roleList, false);
objectMap.put(roleBeanKey, tmp);
}
}
if(isProfileBeanChecked() != null){
if(isProfileBeanChecked()){
Collection<ProfileBean> profileList = this.getProfileBeanService().getListProfileBean();
String tmp = this.getProfileBeanService().exportIntoJson(profileList, false);
objectMap.put(profileBeanKey, tmp);
}
}
//String dataToJSON = this.getRootExportBeanService().exportToJsonFile(objectMap, prettyPrint);
String dataToJSON = this.getRootExportBeanService().exportToJsonFile(objectMap, true);
InputStream anInputStream = new ByteArrayInputStream(dataToJSON.getBytes("UTF-8"));
this.setInputStream(anInputStream);
byte[] bytes = this.inputStreamToByte(this.getInputStream());
this.sendDocumentToResponse(getServletResponse(), "testExport.txt", bytes);
}
catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (StsException e) {
this.addActionError("ihm.message.internalError");
} /*catch (ServletException e) {
this.addActionError("ihm.message.servletError");
//e.printStackTrace();
}*/
this.addActionInfo("ihm.message.dataExported");
return SUCCESS;
}
private void sendDocumentToResponse(HttpServletResponse response, String txtFile, byte[] documentTextBytes) {
OutputStream responseOutputStream = null;
int taille;
StringBuffer fileName;
try {
taille = documentTextBytes.length;
fileName = new StringBuffer();
fileName.append(txtFile);
//fileName.append(".txt");
response.setContentLength(taille);
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName.toString() + "\"");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "cache");
response.setHeader("Cache-control", "must-revalidate");
responseOutputStream = response.getOutputStream();
responseOutputStream.write(documentTextBytes);
responseOutputStream.flush();
responseOutputStream.close();
} catch (Exception e) {
try {
if (responseOutputStream != null) {
responseOutputStream.close();
}
} catch (IOException e1) {
}
}
}
/**
* @param inputStream the inputStream to set
*/
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
/**
* @return the inputStream
*/
public InputStream getInputStream() {
return inputStream;
} |
Partager