Frage

i have a result set which has got some values, i want to export the data which is there in result set as a text file with the save dialog.

how to do this in java?.

I have done the above requirement for excel and java like the following.

response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".xls\"");

and

response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".pdf\""); response.setContentType("application/pdf");

UPDATE :

if(exportTo.equals("text")){
            response.setContentType("text/plain");
            response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".txt\"");
            try {

            } catch (Exception e) {
                // TODO: handle exception
            }

        }

in this

in try block how to set the contents which are avilable from resultset to a output stream and make it available.

War es hilfreich?

Lösung

the only difference is this :

response.setContentType("text/plain");

You can see a full example here ( http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/ )

UPDATE

This is a demo that I have developed and tested and works perfectly fine:

public class ServletDownloadDemo extends HttpServlet {
    private static final int BYTES_DOWNLOAD = 1024;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                                                                                       IOException {
        response.setContentType("text/plain");
        response.setHeader("Content-Disposition", "attachment;filename=downloadname.txt");

        String s = "Test\n\nText file contects!!";
        InputStream input = new ByteArrayInputStream(s.getBytes("UTF8"));

        int read = 0;
        byte[] bytes = new byte[BYTES_DOWNLOAD];
        OutputStream os = response.getOutputStream();

        while ((read = input.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }
        os.flush();
        os.close();
    }
}

It downloads a text file named downloadname.txt with the contents of String s.

UPDATE 2

    String s = "";
    while (rs.next()) {
        s += rs.getString("column_name");
    }

    if (exportTo.equals("text")) {
        response.setContentType("text/plain");
        response.setHeader("Content-Disposition", "attachment;filename=downloadname.txt");

        try {
            InputStream input = new ByteArrayInputStream(s.getBytes("UTF8"));

            int read = 0;
            byte[] bytes = new byte[BYTES_DOWNLOAD];
            OutputStream os = response.getOutputStream();

            //data form resultset

            while ((read = input.read(bytes)) != -1) {
                os.write(bytes, 0, read);
            }
            os.flush();
            os.close();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

You have to populate your ResultSet and place what you need in String s. That's all.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top