Stack Exchange
Download specific rows from database as csv file in struts
I need Java code for for downloading specific rows from database, in CSV format. I'm using the Netbeans IDE and Structs.
I used the following code in my action class:
else if (mode.equalsIgnoreCase("download"))
{
    if(null !=request.getParameter("batchid")){
        batch_id=request.getParameter("batchid");
    }
     UploadVO uploadvo= new UploadVO();          
        filetype = uploadform.getFiletype();
        filename = uploadform.getFilename(); 
    try{
        String sqls = "select ROW_NUMBER() over(order by id) as  row,trx_batch_id,account_id,"
                    + "(select name from [user] where id=tt.user_id) as Uploadedby,(SELECT CONVERT(VARCHAR(10),GETDATE(),101) ) as date_of_transaction,"
                    + "amount,(select description from [_transaction_type] where type=tt.type) as type,note,reverse_transaction_id from"
                    + " [transaction] tt  "+ "where trx_batch_id= '" +batch_id+ "'" ;

            Statement stmts = con.createStatement();

            ResultSet rss = stmts.executeQuery(sqls);
            while (rss.next()) {

            byte[] ba;
            Blob b = rss.getBlob("content");
            ba = b.getBytes(1, (int) b.length());
            uploadvo.setDocfile(ba);
            }
    }
    catch(Exception e){

    }
   if (filetype.trim().equalsIgnoreCase("text/plain")) {
        response.setContentType("text/plain");
    } else if (filetype.trim().equalsIgnoreCase("application/ms-doc")) {
        response.setContentType("application/ms-doc");
    } else if (filetype.trim().equalsIgnoreCase("application/ms-excel")) {
        response.setContentType("application/ms-excel");
    } else if (filetype.trim().equalsIgnoreCase("pdf")) {
        response.setContentType("application/pdf");
    } else if (filetype.trim().equalsIgnoreCase("ppt")) {
        response.setContentType("application/ppt");
    } else if (filetype.trim().equalsIgnoreCase("application/vnd.ms-excel")) {
        response.setContentType("application/vnd.ms-excel");
    } else if (filetype.trim().equalsIgnoreCase("image/jpeg")) {
        response.setContentType("image/jpeg");
    } else {
        response.setContentType("application/octet-stream");
    }


    String disHeader = "Attachment; Filename= " + filename + "";
    System.out.println(disHeader);
    response.setHeader("Content-Disposition", disHeader);
    response.setHeader("cache-control", "no-cache");

    ServletOutputStream outs = response.getOutputStream();
    outs.write(uploadvo.getDocfile());
    outs.flush();
    outs.close();
    return mapping.findForward("success");

}

but I'm getting a null pointer exception.

Please include the stack trace for the NullPointerException - it will tell you the line number the error occurred on and will help people answer you question much more easily.

You're calling fileType.trim() without checking if it's null or not.
Things you're not asking for, but should be mentioned.
Apache Commons has a class StringUtils which is great for checking if strings are empty, 
so instead of writing 
if (null !=request.getParameter("batchid"))

you'd write
if (StringUtils.isNotEmpty(request.getParameter("batchid"))

Since you're probably interested in knowing if it contains something but spaces.
Also, your 
else if (mode.equalsIgnoreCase("download"))

indicates that you're switching on mode (which I'm not sure if you've checked for null either). In struts 1.x, you can use DispatchAction in which you specify a paramter, eg mode, which will be the name of a method called, so in your example you'd implement a method
public ActionForward download(...) {
...
}

which would be called when "download".equals(request.getParameter("mode").
Also note that writing "download".equals(request.getParameter("mode") rather than request.getParameter("mode").equals("download") saves you from a potential null-pointer
