The requirement is to generate a .csv file when click on a hyperlink and after clicking the Open/Save dialog box should open allowing the user to either open the fileor save on the hard disc.
Following piece of code achieves all the above but the problem is when you click on the "open" on the Open/Save dialog box then it pops up again and then when you click on Open gain then the file opens up. Please let me know what needs to be done in the following code snippet to fix this problem:
import java.util.*;
import java.lang.StringBuffer;
import java.lang.Object;
import java.io.*;
import java.util.Random;
import java.util.ArrayList;
FileOutputStream fout;
int i;
Random rand = new Random();
int randomInteger = rand.nextInt();
String sFileName = "Excel"+randomInteger+".csv";
File nameOfFile1 = new File("../ExcelDir");
nameOfFile1.mkdir();
File nameOfFile = new File("../ExcelDir",sFileName);
try{
fout = new FileOutputStream(nameOfFile);
} catch(FileNotFoundException e) {
}
try{
StringBuffer SessionContent = new StringBuffer();
SessionContent.append("Hello").append(",").append("World");
String sFileContent = SessionContent.toString();
byte[] objStrArr = sFileContent.getBytes();
fout.write(objStrArr);
fout.close();
} catch(IOException e) {
}
obj_HttpServletResponse.setContentType("application/vnd.ms-excel");
obj_HttpServletResponse.setHeader("Content-disposition","attachment;filename=\"" +sFileName+ "\"");
InputStream in = new FileInputStream(nameOfFile);
ServletOutputStream outs = obj_HttpServletResponse.getOutputStream();
int bit = 256;
try {
while ((bit) >= 0) {
bit = in.read();
if(bit==-1)
{
break;
}
outs.write(bit);
}
} catch (IOException e) {
}
outs.flush();
outs.close();
in.close();
}