user1260928
user1260928

Reputation: 3439

Spring MVC - Display a PDF file into web browser

I am trying to display a PDF file in a web browser with spring MVC.

public void displayActiviteFiles (Activite activite, HttpServletResponse response) throws IOException {
    File file = new File(activite.getLienUploadUn());
    FileInputStream inputStream = new FileInputStream(file);
    IOUtils.copy(inputStream, response.getOutputStream());
    response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
    response.setContentType("application/pdf");
    response.flushBuffer();
}

But I am getting weird characters instead of the pdf content.
Where am I wrong?

Upvotes: 1

Views: 5707

Answers (3)

enesoral
enesoral

Reputation: 583

You can display including this code.

@GetMapping(value = "/pdf")
public void showPdf(HttpServletResponse response) throws IOException {
    response.setContentType("application/pdf");
    InputStream inputStream = new FileInputStream(new File("/sample.pdf"));
    int nRead;
    while ((nRead = inputStream.read()) != -1) {
        response.getWriter().write(nRead);
    }
} 

Upvotes: 1

sam
sam

Reputation: 2004

If it matters to anyone, then this is the 100% working solution in spring boot 1.5.11. Haven't run on manual Spring MVC projects. Maybe with some minor tweaks, can make it work.

    @GetMapping(value = "/downloadFile")
    public StreamingResponseBody getSteamingFile(HttpServletResponse response) throws URISyntaxException {

        File file = new File(getClass().getClassLoader().getResource("templates/more/si.pdf").toURI());

        //viewing in web browser
        response.setContentType("application/pdf");
        //for downloading the file directly if viewing is not possible
        response.setHeader("Content-Disposition", "inline; filename=" + file.getName());

        file = null;

        //put the directory architecture according to your target directory
        // generated during compilation in maven spring boot
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("templates/more/si.pdf");

        return outputStream -> {
            int nRead;
            byte[] data = new byte[1024];
            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                outputStream.write(data, 0, nRead);
            }
            inputStream.close();
        };
    }

Upvotes: -1

user1260928
user1260928

Reputation: 3439

To answer my question and help some others in my case, this works :

File file = new File(activite.getLienUploadUn());
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1)
{
      baos.write(buffer, 0, bytesRead);
}
response.setHeader("Content-Disposition","inline; filename=\""+file.getName()+"\"");
response.setContentType("application/pdf");
ServletOutputStream outputStream = response.getOutputStream();
baos.writeTo(outputStream);
outputStream.flush();

Upvotes: 5

Related Questions