日期:2014-05-19  浏览次数:20731 次

Java 导出数据?
Java code

    // 导出表数据
    public String exportTable() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        
        //response.setCharacterEncoding("UTF-8");
        // 取得输出流
        OutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        
        String tableName = request.getParameter("msg");// 从页面上获取所有的表名和其标题

        System.out.println(tableName);
        String[] tableNames = StringUtils.split(tableName, ",");
        System.out.println("======================================="
                + tableNames.length);
        for (int i = 0; i < tableNames.length; i++) {
            System.out.println(tableNames[i]);
        }

        //取得exportBackUP方法的返回值
        String backUpString = this.sjkbfService.exportBackUp(tableNames);

        System.out.println("写入的文件:"+backUpString);
        
        String fileDate = new SimpleDateFormat("yyyyMMddHHmmss")
                .format(new Date());
        
        
        String fileName = fileDate + ".backUp";
        
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName); // filename指定默认的名字
        
        //OutputStream bos = response.getOutputStream();
        //OutputStream bos = new BufferedOutputStream(out);
        try {
            
            bos.write(backUpString.getBytes("UTF-8"));
             
            System.out.println("写入成功");
            
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bos.close();
        out.close();
        response.flushBuffer();// 强行将响应缓存中的内容发送到目的地

        
        return null;

    }



后台也打印了写入成功
System.out.println("写入成功");

就是要弹出一个下载框,显示下载路径??没有弹出框???

------解决方案--------------------
.backUp的文件类型,浏览器无法识别。需要设置文件的MIME类型
------解决方案--------------------
1、在代码中加入:
 response.addHeader("Content-Type", "application/octet-stream");



2、或者在WEB服务器的配置中添加文件后缀名与MIME类型的映射。如tomcat在\apache-tomcat-6.0.26\conf\web.xml的<mime-mapping>结点中配置。

------解决方案--------------------
你介个貌似只是将数据写到文件中。
使用SmartUpload试下
------解决方案--------------------
http://topic.csdn.net/u/20120316/17/ff3335d2-91a5-4baa-b41e-2413d15fc67e.html?8720
------解决方案--------------------
探讨

Java code
String fileName = fileDate + ".xml";

response.reset();
//response.setContentType("application/x-msdownload");
//response.setHeader("Location", file……