Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring OutputStream - download pptx with IE

I use this Java code to download files from a web application:

 @RequestMapping(value = "/filedownloads/filedownload/{userid}/{projectid}/{documentfileid}/{version}/", method = RequestMethod.GET)
 public void filesDownload(final @PathVariable("userid") String userId, final @PathVariable("projectid") String projectId,
        final @PathVariable("documentfileid") String documentFileId, final @PathVariable("version") String version,
        final HttpServletResponse response) throws IOException, BusinessException {

    ...

    final String fileName = "filename=" + documentFile.getFileName();
    final InputStream is = new FileInputStream(filePath);
    response.setHeader("Content-Disposition", "inline; " + fileName);
    IOUtils.copy(is, response.getOutputStream());
    response.flushBuffer();
}

if I will download a pptx- file I get the following IE- page:

opend pptx in IE

What I want to do is to open the downloaded file in Powerpoint. My question now would be if there is a header setting in order to open this file with the right application (in this case Powerpoint)

like image 537
quma Avatar asked Oct 03 '16 09:10

quma


2 Answers

Simply try to set the Content Type header properly which is application/vnd.openxmlformats-officedocument.presentationml.presentation in case a pptx, as next:

response.setContentType(
    "application/vnd.openxmlformats-officedocument.presentationml.presentation"
);
response.setHeader(
    "Content-Disposition", 
    String.format("inline; filename=\"%s\"", documentFile.getFileName())
);
response.setContentLength((int) new File(filePath).length());

Here is the list of mime types corresponding to Office 2007 documents.

like image 154
Nicolas Filotto Avatar answered Nov 11 '22 08:11

Nicolas Filotto


Here is a little sample code from a Spring MVC Controller:

@RequestMapping("/ppt")
public void downloadPpt(HttpServletRequest request,  HttpServletResponse response) throws IOException {
    Resource resource = new ClassPathResource("Presentation1.pptx");

    InputStream resourceInputStream = resource.getInputStream();
    response.setHeader("Content-Disposition", "attachment; filename=\"Presentation1.pptx\"");
    response.setContentLengthLong(resource.contentLength());

    byte[] buffer = new byte[1024];
    int len;
    while ((len = resourceInputStream.read(buffer)) != -1) {
        response.getOutputStream().write(buffer, 0, len);
    }

}

By setting the Content-Disposition to attachment, you're telling the browser to download this file as an attachment and by supplying the correct file name with extension, you're telling the Operating System to use whatever application the user normally uses to open a file of this type. In this case it will be MS Power Point.

This way you can get away with not knowing exactly what version of Power Point the file was created with.

like image 1
Akshay Avatar answered Nov 11 '22 08:11

Akshay