Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Open/Save dialog Box appears twice

 
Gavi Raaghav
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really think that this:



Is a good way to deal with exceptions?
 
Gavi Raaghav
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just stripped out that exception handling peice of code as that is not relevent to the question which i am asking.
 
Gavi Raaghav
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just stripped that piece of code as it was not relevant to the question which i have posted
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I beg to differ -- it is relevant. If an exception were to occur in the code you posted, it would silently fail. It's important that when aking a question you post the exact code that you are using or people will have their time wasted chasing down issues and problems that don't really exist.

You should always factor the code down into the smallest unit that exhibiits the problem, but then you should post the complete and exact code that shows the issue.
 
Gavi Raaghav
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to solve this problem but now encountering a diff problem. When on windows the open/save dialog box opens and so does the excel but on a linux box although the open/save dialog box opens once but the excelopens in the browser itself.
Is that a linux problem?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic