Davinho
Davinho

Reputation: 107

Spring web application save file to server

I build a web application.

I would like to read files from server, then generate PDF file ( with itText) then save it to the server.

I don't know how to locate the files from the server then save the file to the server.

I read from my PC and write data to my PC perfectly.

The above code works properly but just on my computer not at server.

String jspPath = "C:\\Users\\dave\\Desktop\\eclipse\\project\\";
    String fileName = "CV.txt";
InputStreamReader ir = new InputStreamReader(new FileInputStream(jspPath+filename), "UTF-8");

// Then generate the PDF with iText //and

FileOutputStream fs = new FileOutputStream(jspPath+"generated.pdf");
    PdfWriter pdfWriter = new PdfWriter(fs);
    PdfDocument pdfdoc = new PdfDocument(pdfWriter);

JSP path references to my folder not the link with the generated pdf.

I would like :

  1. put CV.txt to the server and read it.

  2. Generate pdf ( it will work).

  3. Save the generated PDF to server

  4. A link to the generated PDF which i can download.

Thanks in Advance

Upvotes: 2

Views: 1955

Answers (1)

Keyur Kamdar
Keyur Kamdar

Reputation: 46

Here are few things which might help you.

  1. You can use FormData to pass text file from Frontend to Backend. Use ajax post call to pass data.

you will have entire file on backend in the RequestContext parameter as FileItem object. you can start reading file using InputStreamReader.

  1. than convert it into pdf file.

  2. you can save pdf file to java temporary directory

String temporaryDir = System.getProperty("java.io.tmpdir");

this will return path for java temporary directory and you can delete this pdf file later

  1. you will have to create ResponseBuilder with content-type='application/pdf' to download as a pdf file and return it to the UI. read this post

Hope this information helps you to solve your issue!

Upvotes: 3

Related Questions