---
title: "Seam下载文件的问题"
author: "stenlylee"
date: "2009-08-11T18:24:44Z"
tags: []
slug: "seam"
draft: false
lastmod: "2009-08-11T18:24:44Z"
id: 444944
url: "https://stenlylee.iteye.com/blog/444944"
---

Seam默认下载文件会变成直接在网页中打开内容，主要是因为Response header的问题，需要手工调节下，大概思路如下：

 

```
public String download() {   
        Attachment attachment = entityManager.find(Attachment.class, attachmentId);   
        HttpServletResponse response = (HttpServletResponse)extCtx.getResponse();   
        response.setContentType(attachment.getContentType());   
                response.addHeader("Content-disposition", "attachment; filename=\"" + attachment.getName() +"\"");   
        try {   
            ServletOutputStream os = response.getOutputStream();   
            os.write(attachment.getData());   
            os.flush();   
            os.close();   
            facesContext.responseComplete();   
        } catch(Exception e) {   
            log.error("\nFailure : " + e.toString() + "\n");   
        }   
  
        return null;   
    }  
```

 不过这样似乎也有问题，要把文件全部读入内存然后在下载……

 

不知道有人知道其他办法没，有待补充