Wiki Sayfaları
Abstract Class
Array
Class Compositions and Associations(Siniflarin Olusumu ve Birlikteliyi)
Compiling and Interpreting Java Code(Java Kodunu Derleme)
Degiskenler(Variables)
ECLIPSE & JIRA & MYLYN Üçlüsü
Enum
Extends Olunan Class’larda Constructor iliskisi
Final
HIBERNATE : Repeated column in mapping for entity: ... (should be mapped with insert=false update=
Hibernate ile @Any kullanımı
Initialization Blocks(Class’ta Bloglarin Calisma Prensibi )
instanceof keyword
Interface Class
JAVA PACKAGE
java.io.Serializable
java.util.regex.Matcher and java.util.regex.Pattern
java.util.Scanner and java.util.Formatter
Jsf 2.0 ile Custom Component OlustrumaJsf 2.0 ile Custom Component Olustruma
Labeled Statements
Object
Passing Variables into Methods
Primitive type(Ilkel veri tipleri)
Programming with Java Operators and Strings(Java Operatorleri ve String mothodlari)
Programming with Java Statements(Java Programlama Ifadeleri)
Raporu Ziplenmesi ve Zipten Cikarilmasi
String , StringBuffer , StringBuilder
System.out.printf
TDD ile Uygulama Geliştirme
Tomcatde JSESSIONID Yönetimi
TreeSet Uygulamasi
Varargs
Wicket EKÜ (Eller Klavye Üzerinde) çalışma sorularıReklamlar
Raporu Ziplenmesi ve Zipten Cikarilmasi
Etiketler: Raporlama
Raporu Ziplenmesi ve Zipten Cikarilmasi
raporu-ziplenmesi-ve-zipten-cikarilmasi
Oncelikle IReport ile reporumuzu olusturdum ben raporumun adini reportEmployee yazdim sonra kediniz istediginiz sikilde dizayn ediyorsunuz raporunuzu ve kaydediyorsunuz..
Simdi gelelim bu yazilan uygulamanin calisma prensibine bu bu uygulamaya oncelile reporun yolunu veriyorsunuz
File fileReport = new File("d:/reportEmployee.jrxml"); ve daha sonra olusturdugumuz ReportParser class'i devreye girerek raporu aliyor once resim ve subreportlari alim zip yaziyor ve daha sonrada reporun kendisini yaziyor.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReportParser {
private Properties fileTable;
private String reportDirectory;
private File file;
private ByteArrayOutputStream streamBuffer;
private ZipOutputStream outputStream;
public ReportParser(File file, String reportDirectory) throws Exception {
this.file = file;
this.reportDirectory = reportDirectory;
if (this.reportDirectory == null) {
this.reportDirectory = file.getParent();
}
}
public byte[] getContent() {
return streamBuffer.toByteArray();
}
private String convertToFullName(String directory, String fileName) {
if (!fileName.contains(File.separator)) {
fileName = directory + File.separator + fileName;
}
return fileName;
}
public void parse() throws Exception {
fileTable = new Properties();
streamBuffer = new ByteArrayOutputStream(1024 * 300);
outputStream = new ZipOutputStream(streamBuffer);
parse(file, true);
outputStream.close();
}
public void parse(File file) throws Exception {
parse(file, false);
}
private void parse(File file, boolean isMain) throws Exception {
if (fileTable.getProperty(file.getName()) != null) {
return;
}
String reportName = isMain ? "main.jrxml" : UUID.randomUUID().toString().replaceAll("-", "") + ".jrxml";
fileTable.setProperty(file.toString(), reportName);
Document doc = getDocument(file);
NodeList imgList = doc.getElementsByTagName("imageExpression");
for (int i = 0; i < imgList.getLength(); i++) {
Node imgNode = imgList.item(i);
String fileName = imgNode.getTextContent().replaceAll(""", "");
fileName = convertToFullName(file.getParent(), fileName);
String value = fileTable.getProperty(fileName);
if (value == null) {
value = UUID.randomUUID().toString().replaceAll("-", "") + "." + new File(fileName).getName().split(".")[1];
fileTable.setProperty(fileName, value);
//Image end write
ZipEntry entry = new ZipEntry(value);
entry.setComment(isMain ? "Main report file" : "Subreport file");
outputStream.putNextEntry(entry); //
FileInputStream imageInput = new FileInputStream(fileName);
byte[] imageBytes = new byte[imageInput.available()];
imageInput.read(imageBytes);
outputStream.write(imageBytes);
}
imgNode.getFirstChild().setNodeValue(""" + value + """);
}
NodeList subReportList = doc.getElementsByTagName("subreportExpression");
for (int i = 0; i < subReportList.getLength(); i++) {
Node subReportNode = subReportList.item(i);
String fileName = subReportNode.getFirstChild().getNodeValue().replaceAll(""", "").replaceAll(".jasper", ".jrxml").replaceAll("$P{SUBREPORT_DIR} + ", "");
fileName = convertToFullName(file.getParent(), fileName);
String value = fileTable.getProperty(fileName);
if (value == null) {
parse(new File(fileName));
}
value = fileTable.getProperty(fileName);
subReportNode.getFirstChild().setNodeValue(""" + value.replaceAll("jrxml","jasper") + """);
}
ZipEntry entry = new ZipEntry(reportName);
entry.setComment(isMain ? "Main report file" : "Subreport file");
outputStream.putNextEntry(entry);
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(outputStream));
}
private Document getDocument(File xmlFile) throws Exception {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
return doc;
}
public Properties getFileTable() {
return fileTable;
}
public void setFileTable(Properties fileTable) {
this.fileTable = fileTable;
}
}
public class MainTest{
public static void main(String args[]){
File fileReport = new File("d:/reportEmployee.jrxml");
FileOutputStream fout = null;
File fileZip = new File("d:/Deneme.zip");
ReportParser parser = new ReportParser(fileReport, null);
parser.parse();
byte[] content = parser.getContent();
fout = new FileOutputStream(fileZip);
fout.write(content);
}
}
Asagidaki kod ile reporumuzu zipten cikariyoruz bu kod bilinen zipten cikarma islemi onun icin anlatmaya ihtiyac duymadim
public class UnZipMain{
public static void main(String args[]){
try{
File file = new File("D:/Deneme.zip");
File unZipfile;
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(file);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
int count;
byte data[] = new byte[BUFFER];
unZipfile = new File("D:/Reports/");
FileOutputStream fos = new FileOutputStream(unZipfile + "/" + entry.toString());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
Abdulkadir Selcukoglu
Java Developer
R.I.S.K Company
Azerbaycan/Baku
Yeni Sayfa Oluştur
Tartışma
ya da