Java.io实现简易文件下载

     下载功能。
     项目上常见的下载附件功能,用java来处理io,通过response把文件流返回给页面上。我们需要拿到httpServletResponse对象来做文章。
     如果,页面不render数据,则会直接提示“下载xxx文件...”(如下图) ,如果页面要对文件流进行render处理,比如imag文件render在html上,则将文件流指定给img标签的src即可,这样image标签会把流处理后render出来。
   
     文件代码:
     <html>上使用image标签,使用expression可以控制当宽高超过多少时,进行自动自动适应宽高。

     <div data-dojo-attach-point="imageViewPane" data-dojo-type="dijit.layout.ContentPane" style="overflow-x: hidden;overflow-y: hidden;display:none">
            <image src="" data-dojo-attach-point="viewImage"  style="height: 380px;  height: expression(this.width > 380 ? 380:true);max-width:800px; width: expression(this.width > 800 ? 800: true);" >
     </div>


   /**
     * 下载主附件-(用于在线预览)
     *
     * @param osName
     * @param docId
     * @param response
     * @param request
     * @throws EcmException
     */
    public static void downloadPrimaryAttach(String osName, String docId, HttpServletResponse response, HttpServletRequest request) throws EcmException {
        EntityManager em = null;
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            em = DaoManager.getEntityManager();
            ObjectStore os = ObjectStoreHelper.getObjectStore(osName);
            Document doc = P8DocumentDao.fetchDocumentById(os, docId);
            Collection<ContentTransfer> collection = EngineCollectionUtils.c(doc.get_ContentElements(), ContentTransfer.class);
            if (collection.size() == 0) {// 没有附件
                throw new EcmException(ErrorCode.ATTACHMENT_NULL_ERROR, "attach is null error ", new Exception("attach is null error"));
            }
            Iterator<ContentTransfer> it = collection.iterator();
            ContentTransfer content = it.next();
            String fileName = AttachmentMapper.getAttachmentNameFromRetrievalName(content.get_RetrievalName());
            InputStream in = content.accessContentStream();
            String mineType = BatchImportService.getMineType(fileName);
            response.setContentType(mineType);
            fileName = DownloadUtils.getEncodedFilename(fileName, request);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            byte[] b = new byte[1024];
            int i = -1;
            while ((i = in.read(b)) != -1) {
                  out.write(b);
            }
        } catch (EcmException e) {
            logger.error("downloadAttach error", e);
            throw new EcmException(e);
        } catch (Exception e) {
            logger.error("downloadAttach error", e);
            throw new EcmException(ErrorCode.ATTACHMENT_DOWNLOAD_ERROR, "downloadAttach error ", e);
        } finally {
            if (em != null) {
                em.close();
            }
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (Exception ex) {
                    logger.error(ex);
                }
            }
        }
    }
Java.io实现简易文件下载_第1张图片

附加:

Office 2007 File Format MIME Types for HTTP Content Streaming

http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx

Ext

MIME Type

.doc

application/msword

.dot

application/msword

.docx

application/vnd.openxmlformats-officedocument.wordprocessingml.document

.dotx

application/vnd.openxmlformats-officedocument.wordprocessingml.template

.docm

application/vnd.ms-word.document.macroEnabled.12

.dotm

application/vnd.ms-word.template.macroEnabled.12

.xls

application/vnd.ms-excel

.xlt

application/vnd.ms-excel

.xla

application/vnd.ms-excel

.xlsx

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

.xltx

application/vnd.openxmlformats-officedocument.spreadsheetml.template

.xlsm

application/vnd.ms-excel.sheet.macroEnabled.12

.xltm

application/vnd.ms-excel.template.macroEnabled.12

.xlam

application/vnd.ms-excel.addin.macroEnabled.12

.xlsb

application/vnd.ms-excel.sheet.binary.macroEnabled.12

.ppt

application/vnd.ms-powerpoint

.pot

application/vnd.ms-powerpoint

.pps

application/vnd.ms-powerpoint

.ppa

application/vnd.ms-powerpoint

.pptx

application/vnd.openxmlformats-officedocument.presentationml.presentation

.potx

application/vnd.openxmlformats-officedocument.presentationml.template

.ppsx

application/vnd.openxmlformats-officedocument.presentationml.slideshow

.ppam

application/vnd.ms-powerpoint.addin.macroEnabled.12

.pptm

application/vnd.ms-powerpoint.presentation.macroEnabled.12

.potm

application/vnd.ms-powerpoint.presentation.macroEnabled.12

.ppsm

application/vnd.ms-powerpoint.slideshow.macroEnabled.12






你可能感兴趣的:(Java.io实现简易文件下载)