I wanted to do the same in my web app, and also tried frist with JFileChooser, also got the problem as you. Then I solved this other way using "content - disposition". How it works: User click on button/link in my app, controller fetches something from database and write it to outputstream. User is asked if he wants to open this file or save it on computer. Here is how I done that in my app:
@Controller
public class ExportPhonebook extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
private PhoneBookService phoneBookSer;
private void setResponseHeaderTXT(HttpServletResponse response) {
response.setContentType("text/plain; charset=UTF-8");
response.setHeader("content-disposition", "attachment; filename=imenik.txt" );
}
@RequestMapping(value = "/exportPhonebook.html", method = RequestMethod.POST)
public void exportPhonebook(Model model, HttpServletResponse response, HttpServletRequest request) {
List<User> listOfAllUsers = phoneBookSer.fetchAllUsers();
String imenik = "";
for (User user : listOfAllUsers) {
imenik = imenik + user.getPrezime() + " " + user.getIme() + ", Organizacijska jedinica: " + user.getOrganizacijskaJedinica() +
", Telefon: " + user.getTelefon() + ", Faks: " + user.getTelefaks() + ", Email: " + user.getEmail() +
", Lokacija: " + user.getLokacija() + ", Kat: " + user.getKat() + ", Soba: " + user.getBrojSobe() + ";\r\n" ;
}
try {
setResponseHeaderTXT(response);
OutputStream outputStream = response.getOutputStream();
outputStream.write(imenik.getBytes(Charset.forName("UTF-8")));
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I have done similar thing with pdf files:
@Controller
public class ExportPhonebook extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
private PhoneBookService phoneBookSer;
private void setResponseHeaderPDF(HttpServletResponse response) {
response.setContentType("application/pdf");
response.setHeader("content-disposition", "attachment; filename=imenik.pdf" );
}
@RequestMapping(value = "/exportPhonebook.html", method = RequestMethod.POST)
public void exportPhonebook(Model model, HttpServletResponse response, HttpServletRequest request) {
try {
setResponseHeaderPDF(response);
Document document = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter pdfWriter = null;
pdfWriter = PdfWriter.getInstance(document, baosPDF);
PageNumbersEventHelper events = new PageNumbersEventHelper();
pdfWriter.setPageEvent(events);
document.open();
addMetaData(document);
addTitlePage(document);
ServletContext servletContext = request.getSession().getServletContext();
String relativeWebPath = "img/image.png";
String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);
Image image = Image.getInstance(absoluteDiskPath);
image.setAbsolutePosition(450, 700);
document.add(image);
addContent(document);
document.close();
pdfWriter.close();
OutputStream os = response.getOutputStream();
baosPDF.writeTo(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I didn't copy several methods in which I create content for my pdf file, but I think that you don't need that.