package demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.logging.Logger;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
@ManagedBean(name="downFile", scope=ManagedBeanScope.SESSION)
public class DownFile {
public String download() {
//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"));
response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));
// log.debug(contentDisposition.toString());
ServletOutputStream out = response.getOutputStream();
//log.debug(new Long(oaMailAttach.getAtFile().length()));
byte[] bytes = new byte[0xffff];
InputStream is = new FileInputStream(new File(path + fileName));
int b = 0;
while ((b = is.read(bytes, 0, 0xffff)) > 0) {
out.write(bytes, 0, b);
}
is.close();
out.flush();
ctx.responseComplete();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return null;
}
}