2016-10-03 15 views
5

tôi sử dụng mã Java này để tải tập tin từ một ứng dụng web:Xuân OutputStream - tải pptx với IE

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

nếu tôi sẽ tải về một tập tin pptx- tôi nhận được trang tức là-sau:

opend pptx in IE

Điều tôi muốn làm là mở tệp đã tải xuống trong Powerpoint. Câu hỏi của tôi bây giờ sẽ là nếu có một tiêu đề thiết lập để mở tập tin này với các ứng dụng bên phải (trong trường hợp này Powerpoint)

Trả lời

2

Đơn giản chỉ cần cố gắng thiết lập các Content Type tiêu đề đúng là application/vnd.openxmlformats-officedocument.presentationml.presentation trong trường hợp một pptx, như sau :

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.

1

Dưới đây là một chút mẫu mã từ một MVC Controller mùa xuân:

@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); 
    } 

} 

Bằng cách đặt Content-Disposition thành attachment, bạn đang yêu cầu trình duyệt tải xuống tệp này dưới dạng tệp đính kèm và bằng cách cung cấp tên tệp chính xác với tiện ích, bạn đang yêu cầu Hệ điều hành sử dụng bất kỳ ứng dụng nào mà người dùng thường sử dụng để mở một tệp thuộc loại này. Trong trường hợp này, nó sẽ là MS Power Point.

Bằng cách này bạn có thể thoát ra ngoài mà không biết chính xác phiên bản của Power Point mà tệp được tạo bằng.

0

Tôi đã kiểm tra mã số trong IE-11 hoạt động tốt. Xem mã dưới đây tức là

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

Tạo bytes từ lưu pptx tập tin.

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

Vậy đó, bạn có thể tải xuống pptx tệp. Hy vọng mã giúp bạn, nếu bạn có bất kỳ truy vấn hoặc nghi ngờ sau đó chúng tôi có thể thảo luận hoặc nếu có đề nghị. Cảm ơn bạn

Các vấn đề liên quan