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

附加:

Office 2007 File Format MIME Types for HTTP Content Streaming

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






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是一道光_

1分也是爱,鼓励鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值