-
package demo;
-
-
-
-
import javax. faces. application. Application;
-
import javax. faces. context. FacesContext;
-
import javax. servlet. ServletOutputStream;
-
import javax. servlet. http. HttpServletResponse;
-
-
import org. operamasks. faces. annotation. ManagedBean;
-
import org. operamasks. faces. annotation. ManagedBeanScope;
-
-
public class DownFile {
-
//OaMailAttach oaMailAttach = getOaMailAttach();
-
-
String path= "G:\sun\";
-
String fileName=" 20074111452140. doc ";
-
-
try {
-
FacesContext ctx = FacesContext.getCurrentInstance();
-
ctx.responseComplete();
-
//String contentType = "application/octet-stream;charset=utf -8 ";
-
String contentType = "application/x-download ";
-
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
-
-
-
-
response.setContentType(contentType);
-
-
StringBuffer contentDisposition = new StringBuffer();
-
-
contentDisposition.append("attachment; ");
-
-
contentDisposition.append("filename=\ "" );
-
contentDisposition. append (fileName );
-
contentDisposition. append ( "\"" );
-
//log.debug(System.getProperty("file.encoding"));
-
// log.debug(contentDisposition.toString());
-
ServletOutputStream out = response. getOutputStream ( );
-
-
//log.debug(new Long(oaMailAttach.getAtFile().length()));
-
-
byte [ ] bytes = new byte [0xffff ];
-
int b = 0;
-
while ( (b = is. read (bytes, 0, 0xffff ) ) > 0 ) {
-
out. write (bytes, 0, b );
-
}
-
is. close ( );
-
out. flush ( );
-
ctx. responseComplete ( );
-
// TODO 自动生成 catch 块
-
e. printStackTrace ( );
-
}
-
return null;
-
}
-
-
}
关键语句:ctx.responseComplete();
不然会报错误如下:
Servlet response already use stream, Writer not possible