Spring OutputStream - download pptx from 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 upload a pptx file, I get the following IE page:

opend pptx in IE

What I want to do is open the downloaded file in Powerpoint. My question now will be if there is a header option to open this file with the correct application (in this case Powerpoint)

+5
source share
3 answers

Try setting the Content Type header correctly, which is application/vnd.openxmlformats-officedocument.presentationml.presentation in case of a pptx , as follows:

 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()); 

The following is a list of mime types that correspond to Office 2007 documents.

+2
source

Here is a small sample code from Spring MVC:

 @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 tell the browser to download this file as an attachment and, specifying the correct file name with the extension, you tell the operating system to use any application that the user usually uses to open this type of file. In this case, it will be the MS Power Point.

That way, you can leave without knowing exactly which version of Power Point the file created.

+1
source

I tested the code in IE-11 , its work is great. See below ie code

 @RequestMapping(value = "/downloadfile", method = RequestMethod.GET) @ResponseBody public void downloadfile(HttpServletRequest request, HttpServletResponse response) throws Exception { ServletOutputStream servletOutputStream = null; try { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=downloadppt.pptx"); byte[] ppt = downloadFile(); servletOutputStream = response.getOutputStream(); servletOutputStream.write(ppt); } catch (Exception e) { throw e; } finally { servletOutputStream.flush(); servletOutputStream.close(); } } 

Generate bytes from the saved pptx file.

 public byte[] downloadFile() throws IOException { InputStream inputStream = new FileInputStream(new File("e:/testppt.pptx")); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // Transfer bytes from source to destination byte[] buf = new byte[1024]; int len; while ((len = inputStream.read(buf)) > 0) { byteArrayOutputStream.write(buf, 0, len); } inputStream.close(); byteArrayOutputStream.close(); return byteArrayOutputStream.toByteArray(); } 

To do this, you can download the pptx file. The code of hope will help you if you have any questions or doubts, we can discuss or any suggestions. thank you

0
source

Source: https://habr.com/ru/post/1257639/


All Articles