• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Open/Save dialog Box appears twice

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic