欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp 导出文件实例代码

程序员文章站 2022-04-11 19:15:04
public ActionForward exportFile(ActionMapping mapping, ActionForm form,...

public ActionForward exportFile(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String natLog = "test";
                
        try {
            StringBuilder fileName = null;
            OutputStream output = null;
            
            byte[] streamBytes = natLog.getBytes();
            if (streamBytes == null) {

            } else {
                fileName = new StringBuilder(64);
                fileName.append("export.txt");
                try {
                    output = response.getOutputStream();
                    
                    FileStream.initResponseAsFile(response, fileName.toString(), "MIME/octet-strea");
                    output.write(streamBytes);
                    output.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (output != null) {
                        output.close();
             &n