Java-PDF is saved but java see only old PDF












0















I have a problem with java. I wanna create PDF file using itextpdf in java spring. Next i wanna send this pdf using email. I run java application and i send email using REST API. My mail is sendin correctly but attachment is old PDF. It is mean that I make request from API and my pdf is created in java and send by email. My new pdf is created by java and i see this but by email java send old pdf which was generated before again run application.



It is my function to create PDF:



public static final String DEST = "src/main/resources/sample2.pdf";
public void createPDF(User user) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
document.add(new Paragraph(user.getEmail()+"aaaaaa"));
document.close();
}


It is my function to send:



public void sendResults(User user)
throws MessagingException, FileNotFoundException, DocumentException {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("test");
helper.setText("test");
helper.setTo(user.getEmail());
helper.setFrom("mail@gmail.com");
helper.addAttachment("sample2.pdf", new
ClassPathResource("sample2.pdf"));
emailSender.send(message);
}


I have two request:
*create this PDF
and
*send mail with pdf



PDF is default save in folder "resources". Everything is great but despite that in java after request "create PDF" is new PDF, method "send mail" send old pdf :/ I do not know, what is wrong. Help me.










share|improve this question




















  • 2





    How are you deploying the app? In general, you don't want to write data to inside the application--if it's a WAR file you can't anyway. Save it somewhere outside of the app.

    – Dave Newton
    Nov 25 '18 at 15:57






  • 1





    If you just want to send this PDF immediately after generate it, you can save it to ByteArrayOutputStream and just hold it in memory if the PDF is light-weight.

    – B_Osipiuk
    Nov 25 '18 at 16:06











  • It is app for my study and to learn. It will not be on production, so it is good solve for me, but this with save to ByteArrayOutputStream is very interesting. It is so fast as you write ?

    – Kuba
    Nov 25 '18 at 21:02
















0















I have a problem with java. I wanna create PDF file using itextpdf in java spring. Next i wanna send this pdf using email. I run java application and i send email using REST API. My mail is sendin correctly but attachment is old PDF. It is mean that I make request from API and my pdf is created in java and send by email. My new pdf is created by java and i see this but by email java send old pdf which was generated before again run application.



It is my function to create PDF:



public static final String DEST = "src/main/resources/sample2.pdf";
public void createPDF(User user) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
document.add(new Paragraph(user.getEmail()+"aaaaaa"));
document.close();
}


It is my function to send:



public void sendResults(User user)
throws MessagingException, FileNotFoundException, DocumentException {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("test");
helper.setText("test");
helper.setTo(user.getEmail());
helper.setFrom("mail@gmail.com");
helper.addAttachment("sample2.pdf", new
ClassPathResource("sample2.pdf"));
emailSender.send(message);
}


I have two request:
*create this PDF
and
*send mail with pdf



PDF is default save in folder "resources". Everything is great but despite that in java after request "create PDF" is new PDF, method "send mail" send old pdf :/ I do not know, what is wrong. Help me.










share|improve this question




















  • 2





    How are you deploying the app? In general, you don't want to write data to inside the application--if it's a WAR file you can't anyway. Save it somewhere outside of the app.

    – Dave Newton
    Nov 25 '18 at 15:57






  • 1





    If you just want to send this PDF immediately after generate it, you can save it to ByteArrayOutputStream and just hold it in memory if the PDF is light-weight.

    – B_Osipiuk
    Nov 25 '18 at 16:06











  • It is app for my study and to learn. It will not be on production, so it is good solve for me, but this with save to ByteArrayOutputStream is very interesting. It is so fast as you write ?

    – Kuba
    Nov 25 '18 at 21:02














0












0








0








I have a problem with java. I wanna create PDF file using itextpdf in java spring. Next i wanna send this pdf using email. I run java application and i send email using REST API. My mail is sendin correctly but attachment is old PDF. It is mean that I make request from API and my pdf is created in java and send by email. My new pdf is created by java and i see this but by email java send old pdf which was generated before again run application.



It is my function to create PDF:



public static final String DEST = "src/main/resources/sample2.pdf";
public void createPDF(User user) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
document.add(new Paragraph(user.getEmail()+"aaaaaa"));
document.close();
}


It is my function to send:



public void sendResults(User user)
throws MessagingException, FileNotFoundException, DocumentException {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("test");
helper.setText("test");
helper.setTo(user.getEmail());
helper.setFrom("mail@gmail.com");
helper.addAttachment("sample2.pdf", new
ClassPathResource("sample2.pdf"));
emailSender.send(message);
}


I have two request:
*create this PDF
and
*send mail with pdf



PDF is default save in folder "resources". Everything is great but despite that in java after request "create PDF" is new PDF, method "send mail" send old pdf :/ I do not know, what is wrong. Help me.










share|improve this question
















I have a problem with java. I wanna create PDF file using itextpdf in java spring. Next i wanna send this pdf using email. I run java application and i send email using REST API. My mail is sendin correctly but attachment is old PDF. It is mean that I make request from API and my pdf is created in java and send by email. My new pdf is created by java and i see this but by email java send old pdf which was generated before again run application.



It is my function to create PDF:



public static final String DEST = "src/main/resources/sample2.pdf";
public void createPDF(User user) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
document.add(new Paragraph(user.getEmail()+"aaaaaa"));
document.close();
}


It is my function to send:



public void sendResults(User user)
throws MessagingException, FileNotFoundException, DocumentException {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("test");
helper.setText("test");
helper.setTo(user.getEmail());
helper.setFrom("mail@gmail.com");
helper.addAttachment("sample2.pdf", new
ClassPathResource("sample2.pdf"));
emailSender.send(message);
}


I have two request:
*create this PDF
and
*send mail with pdf



PDF is default save in folder "resources". Everything is great but despite that in java after request "create PDF" is new PDF, method "send mail" send old pdf :/ I do not know, what is wrong. Help me.







java rest spring-mvc spring-boot pdf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 16:14









MTCoster

3,83922141




3,83922141










asked Nov 25 '18 at 15:26









KubaKuba

33




33








  • 2





    How are you deploying the app? In general, you don't want to write data to inside the application--if it's a WAR file you can't anyway. Save it somewhere outside of the app.

    – Dave Newton
    Nov 25 '18 at 15:57






  • 1





    If you just want to send this PDF immediately after generate it, you can save it to ByteArrayOutputStream and just hold it in memory if the PDF is light-weight.

    – B_Osipiuk
    Nov 25 '18 at 16:06











  • It is app for my study and to learn. It will not be on production, so it is good solve for me, but this with save to ByteArrayOutputStream is very interesting. It is so fast as you write ?

    – Kuba
    Nov 25 '18 at 21:02














  • 2





    How are you deploying the app? In general, you don't want to write data to inside the application--if it's a WAR file you can't anyway. Save it somewhere outside of the app.

    – Dave Newton
    Nov 25 '18 at 15:57






  • 1





    If you just want to send this PDF immediately after generate it, you can save it to ByteArrayOutputStream and just hold it in memory if the PDF is light-weight.

    – B_Osipiuk
    Nov 25 '18 at 16:06











  • It is app for my study and to learn. It will not be on production, so it is good solve for me, but this with save to ByteArrayOutputStream is very interesting. It is so fast as you write ?

    – Kuba
    Nov 25 '18 at 21:02








2




2





How are you deploying the app? In general, you don't want to write data to inside the application--if it's a WAR file you can't anyway. Save it somewhere outside of the app.

– Dave Newton
Nov 25 '18 at 15:57





How are you deploying the app? In general, you don't want to write data to inside the application--if it's a WAR file you can't anyway. Save it somewhere outside of the app.

– Dave Newton
Nov 25 '18 at 15:57




1




1





If you just want to send this PDF immediately after generate it, you can save it to ByteArrayOutputStream and just hold it in memory if the PDF is light-weight.

– B_Osipiuk
Nov 25 '18 at 16:06





If you just want to send this PDF immediately after generate it, you can save it to ByteArrayOutputStream and just hold it in memory if the PDF is light-weight.

– B_Osipiuk
Nov 25 '18 at 16:06













It is app for my study and to learn. It will not be on production, so it is good solve for me, but this with save to ByteArrayOutputStream is very interesting. It is so fast as you write ?

– Kuba
Nov 25 '18 at 21:02





It is app for my study and to learn. It will not be on production, so it is good solve for me, but this with save to ByteArrayOutputStream is very interesting. It is so fast as you write ?

– Kuba
Nov 25 '18 at 21:02












1 Answer
1






active

oldest

votes


















0














Please try this method of loading a file from a resource folder in Java.



ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("sample2.pdf").getFile());



For spring based application you can take advantage of ResourceUtils class.



 File file = ResourceUtils.getFile("classpath:xyz.xml")


or add a field with annotation @Value to your bean:



 @Value("classpath:xyz.xml")
private Resource resource;


And then simply:



 resource.getInputStream();





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468983%2fjava-pdf-is-saved-but-java-see-only-old-pdf%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Please try this method of loading a file from a resource folder in Java.



    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("sample2.pdf").getFile());



    For spring based application you can take advantage of ResourceUtils class.



     File file = ResourceUtils.getFile("classpath:xyz.xml")


    or add a field with annotation @Value to your bean:



     @Value("classpath:xyz.xml")
    private Resource resource;


    And then simply:



     resource.getInputStream();





    share|improve this answer






























      0














      Please try this method of loading a file from a resource folder in Java.



      ClassLoader classLoader = getClass().getClassLoader();
      File file = new File(classLoader.getResource("sample2.pdf").getFile());



      For spring based application you can take advantage of ResourceUtils class.



       File file = ResourceUtils.getFile("classpath:xyz.xml")


      or add a field with annotation @Value to your bean:



       @Value("classpath:xyz.xml")
      private Resource resource;


      And then simply:



       resource.getInputStream();





      share|improve this answer




























        0












        0








        0







        Please try this method of loading a file from a resource folder in Java.



        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("sample2.pdf").getFile());



        For spring based application you can take advantage of ResourceUtils class.



         File file = ResourceUtils.getFile("classpath:xyz.xml")


        or add a field with annotation @Value to your bean:



         @Value("classpath:xyz.xml")
        private Resource resource;


        And then simply:



         resource.getInputStream();





        share|improve this answer















        Please try this method of loading a file from a resource folder in Java.



        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("sample2.pdf").getFile());



        For spring based application you can take advantage of ResourceUtils class.



         File file = ResourceUtils.getFile("classpath:xyz.xml")


        or add a field with annotation @Value to your bean:



         @Value("classpath:xyz.xml")
        private Resource resource;


        And then simply:



         resource.getInputStream();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 16:41

























        answered Nov 25 '18 at 15:32









        Yaniv LevyYaniv Levy

        164




        164
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468983%2fjava-pdf-is-saved-but-java-see-only-old-pdf%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin