2012-08-10 19 views
5

को iTextPDF दस्तावेज़ कन्वर्ट करने के लिए मैं बाइट [] को iTextPDF दस्तावेज़ फ़ाइल को रूपांतरित करने के बाद यह स्मृति में बनने वाले की जरूरत है। मैंने पहले से ही परीक्षण किया है कि मुझे पीडीएफ बनाने के साथ कोई समस्या नहीं है। समस्या यह है कि इसे डीबी में स्टोर करने के लिए बाइट सरणी में कैसे परिवर्तित करें।कैसे बाइट सरणी

Document generatedDocument = reportService.generateRequestForm(scdUser, jsonObject, 0, null); 
reportService.generateRequestForm(scdUser, jsonObject, 0, null); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
PdfWriter pdfWriter = PdfWriter.getInstance(generatedDocument, baos); 
generatedDocument.open(); 
document.setDocument(baos.toByteArray()); // stores as blob 

मैं डेटाबेस ब्लॉब स्तंभ पर अशक्त का मूल्य मिला:

यहाँ मेरी कोड है।

यहाँ मेरी दस्तावेज़ डोमेन वस्तु है:

दस्तावेज़ डोमेन वस्तु

@Entity 
@Table(name = "document") 
public class Document implements java.io.Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "document_id", nullable = false) 
    private int documentId; 
    @Column(name = "document_name", nullable = false, length = 65535) 
    private String documentName; 
    @Column(name = "document_type", nullable = false) 
    private int documentType; 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "upload_date", nullable = false, length = 19) 
    private Date uploadDate = new Date(); 
    @Column(name = "document", nullable = false) 
    private byte[] document; // BLOB COLUMN 
    @Column(name = "document_size", nullable = false) 
    private long documentSize; 
    @Column(name = "title", nullable = true, insertable = true, updatable = true, length = 65535, precision = 0) 
    private String title; 
    @Column(name = "tag", nullable = true, insertable = true, updatable = true, length = 65535, precision = 0) 
    private String tag; 
    @Column(name = "description", nullable = true, insertable = true, updatable = true, length = 65535, precision = 0) 
    private String description; 
    @Column(name = "shared", nullable = false, insertable = true, updatable = true, length = 1, precision = 0) 
    private boolean shared = false; 
    @Column(name = "status", nullable = false) 
    private int status = DocumentStatus.READY.getStatus(); 


    public int getDocumentId() { 
     return this.documentId; 
    } 

    public void setDocumentId(int documentId) { 
     this.documentId = documentId; 
    } 

    public String getDocumentName() { 
     return this.documentName; 
    } 

    public void setDocumentName(String documentName) { 
     this.documentName = documentName; 
    } 

    public int getDocumentType() { 
     return this.documentType; 
    } 

    public void setDocumentType(int documentType) { 
     this.documentType = documentType; 
    } 

    public Date getUploadDate() { 
     return this.uploadDate; 
    } 

    public void setUploadDate(Date uploadDate) { 
     this.uploadDate = uploadDate; 
    } 

    public byte[] getDocument() { 
     return this.document; 
    } 

    public void setDocument(byte[] document) { 
     this.document = document; 
    } 

    public long getDocumentSize() { 
     return this.documentSize; 
    } 

    public void setDocumentSize(long documentSize) { 
     this.documentSize = documentSize; 
    } 

    public String getTag() { 
     return tag; 
    } 

    public void setTag(String tag) { 
     this.tag = tag; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public boolean getShared() { 
     return shared; 
    } 

    public void setShared(boolean shared) { 
     this.shared = shared; 
    } 


    public int getStatus() { 
     return status; 
    } 

    public void setStatus(int status) { 
     this.status = status; 
    } 


} 
+0

प्रश्न को देखें, फिर अपनी अंतिम वाक्य देखें .. – Eugene

+0

ऐसा लगता है कि दस्तावेज़ उस डेटाबेस त्रुटि के कारण पहली बार उत्पन्न नहीं होगा। –

+0

@ यूजीन यह वही है; मैं कॉलम blob करने के लिए बाइट [] बना रहा हूँ। – talha06

उत्तर

2

टिप्पणियों से यह प्रतीत होता है कि यह जिस तरह से पीडीएफ कार्यावधि में उत्पन्न होता है लेना देना नहीं है कि, लेकिन इसके बजाय यह रास्ता डीबी में संग्रहीत है। आपको वह कोड भी प्रदान करना होगा।

यहाँ क्या मैं वास्तव में (अपने परीक्षण) है:

धारक:

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 



@Entity 
public class PDFHolder implements Serializable { 

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
private long id; 

@Column(columnDefinition = "LONGBLOB") 
private byte[] documentPDF; 

public byte[] getDocumentPDF() { 
    return documentPDF; 
} 

public void setDocumentPDF(byte[] documentPDF) { 
    this.documentPDF = documentPDF; 
} 

public long getId() { 
    return id; 
} 

public void setId(long id) { 
    this.id = id; 
} 
} 

भंडार है, जो बचाने करता है:

@Repository 
public interface PDFHolderRepository extends JpaRepository<PDFHolder, Long> {} 

और वास्तविक डालने:

private void generateDatabaseRows() { 

    try{ 
     String urlPDF = "http://cetatenie.just.ro/LinkClick.aspx?fileticket=K13DZkoIE2o%3d&tabid=57&mid=593"; 
     URL url = new URL(urlPDF); 

     ByteBuffer byteBufferResponse = this.getAsByteArray(url.openConnection()); 
     byte [] responseArray = byteBufferResponse.array(); 
     System.out.println("Size of the PDF : " + responseArray.length); 

     // Save it to DB 
     PDFHolder pdfHolder = new PDFHolder(); 
     pdfHolder.setDocumentPDF(responseArray); 

     pdfHolderRepository.save(pdfHolder); 

    } catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 


private ByteBuffer getAsByteArray(final URLConnection urlConnection) throws IOException { 
    final ByteArrayOutputStream tmpOut = new ByteArrayOutputStream(); 
    final InputStream inputStream = urlConnection.getInputStream(); 
    final byte[] buf = new byte[1024]; 
    int len; 
    while (true) { 
     len = inputStream.read(buf); 
     if (len == -1) { 
      break; 
     } 
     tmpOut.write(buf, 0, len); 
    } 
    tmpOut.close(); 
    return ByteBuffer.wrap(tmpOut.toByteArray(), 0, tmpOut.size()); 
} 

@Autowired 
PDFHolderRepository pdfHolderRepository; 

MySQL stores the byte array

+0

पहले पोस्ट संपादित किया गया .. – talha06

+0

@ talha06 हमें बिल्कुल दिखाएं जहां आपको शून्य मिलती है। – Eugene

+0

जब मैं डीबी से दस्तावेज़ पुनर्प्राप्त करता हूं तो मुझे शून्य मिलता है। (जब भी मैं GUI - MySQL वर्कबेंच पर शून्य हूं, तो मैं हाइबरनेट का उपयोग करके डीबी को पीडीएफ बना रहा हूं। जब मैं कोड डीबग करता हूं, तो मुझे लगता है कि दस्तावेज़ सफलतापूर्वक उत्पन्न हुआ है। तो मैं सीखना चाहता हूं कि मैं ठीक से बने रहने के लिए iTextPDF दस्तावेज़ को बाइट सरणी में सफलतापूर्वक कैसे परिवर्तित कर सकता हूं। – talha06

12

मैं एक ऐसी ही समस्या थी ... मैं दस्तावेज़ बनाएंगे, और वर्ग जहां मैं इसे बनाया अंदर, मैं इसे बचा सकता है: बेशक मैं इस वर्ग में एक @Autowired भंडार है फाइल करने के लिए, और यह बहुत अच्छा काम किया। हालांकि, जब मैंने इसे स्ट्रीम के रूप में वापस करने की कोशिश की तो मुझे एक शून्य मूल्य मिलेगा।

समस्या यह थी कि दस्तावेज़ बंद होने के बाद (document.close()), यह स्ट्रीम को भी बंद कर देगा।

जब मैं दस्तावेज़ बनाता हूं और पीडीएफवाइटर आउटपुट करता हूं तो कार्य-आसपास एक बाइटएरे ऑटपुटस्ट्रीम बनाना था। मैं तब भी कर सकता था जो मैं पीडीएफ बाइट्स के साथ चाहता था ... मेरे मामले में मैंने उन्हें उपयोगकर्ता को भेजने के लिए स्ट्रीम किए गए सामग्री में परिवर्तित कर दिया।

बाइट्स धारण करने के लिए एक चर बनाएँ:

Document document = new Document(PageSize.LETTER, 0.75F, 0.75F, 0.75F, 0.75F); 
    PdfWriter.getInstance(document, byteArrayOutputStream); // Do this BEFORE document.open() 

    document.open(); 
    createPDF(document); // Whatever function that you use to create your PDF 
    document.close(); 

एक बार जब आप पैदा काम हो गया:

private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

PdfWriter उत्पादन बाइट [] के लिए डेटा है के रूप में यह दस्तावेज़ बनाता है पीडीएफ, बस बाइट्स प्राप्त करें और जैसा कि आप करेंगे उनके साथ करें।

byte[] pdfBytes = byteArrayOutputStream.toByteArray(); 

मुझे नहीं पता कि आपकी रिपोर्ट सेवा वर्ग कैसा दिखता है, लेकिन यह रखने के लिए यह एक अच्छी जगह हो सकती है।

उम्मीद है कि इससे मदद मिलती है।

+0

ग्रेट उत्तर! मुझे बहुत मदद की! क्या फ़ाइल का नाम सेट करने के लिए कोई शानदार तरीका है? क्रोम इसे स्वचालित रूप से "something.pdf" के रूप में डाउनलोड करता है। –