my dog learned polymorphism*
The moose likes Servlets and the fly likes Open/Save dialog Box appears twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Open/Save dialog Box appears twice" Watch "Open/Save dialog Box appears twice" New topic
Author

Open/Save dialog Box appears twice

Gavi Raaghav
Ranch Hand

Joined: Apr 28, 2005
Posts: 82
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

Joined: Jan 10, 2002
Posts: 61306
    
  66

Do you really think that this:



Is a good way to deal with exceptions?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gavi Raaghav
Ranch Hand

Joined: Apr 28, 2005
Posts: 82
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

Joined: Apr 28, 2005
Posts: 82
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

Joined: Jan 10, 2002
Posts: 61306
    
  66

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

Joined: Apr 28, 2005
Posts: 82
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?
 
GeeCON Prague 2014
 
subject: Open/Save dialog Box appears twice