Solved

Save As Dialog

Posted on 2011-03-15
25
301 Views
Last Modified: 2012-05-11
Hello i have this code that generate me an csv file, this file is saved in the C:\ of my server, I would like to give the user the option to save the file in their machines with a Save as Dialog option

down is the code i use to generate the file


BufferedWriter writer = new BufferedWriter(new FileWriter("C://file.csv"));
 
	       
	         for (int i=0;i<arrayExportXLS.size();i++){
	        	 
	         
	        	 ExportXLS expi = (ExportXLS) arrayExportXLS.get(i); 
		        	 writer.write(expi.printObjectI()); 

		             writer.newLine();
	         }
	       
			 writer.close();

Open in new window

0
Comment
Question by:cano63
  • 14
  • 11
25 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35139652
If you want to use the Save As dialog you should use the JFileChooser comopponent
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35139664
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35139684
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35139717


And this one with a little bit of explanation:
http://www.javaswing.org/jfilechooser-example.aspx
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35139762
If you are writing about web application, then you don't need to worry about it as your browser
will normally open similar finctionality and allow the user to save the file in the local folder, which
user chooses.
0
 
LVL 1

Author Comment

by:cano63
ID: 35147420
I,m working in a web application, is not supposed to be created with java script?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35147626
No, most of the content on the web is actually in HTML, only some more sophisticated functionality is dine in JavaScript. The task you mentioned does nor require JavaScript - when you have a link pointing to your CSV browser will normally show your client  filechooser window - this is part of the bropwser fuincionaltiy/standard configuration
0
 
LVL 1

Author Comment

by:cano63
ID: 35191927
But how can i generate that save file automatic from the file that i create, let said i create the file and automaticly display the user the save dialog of the browser, how i pass my file function
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35192022
You just place the link to your file on your Web page - then the behavior will all depend on the setup of the client browser.
Most browsers are set up in such way that if link points to .html file - it will just display it,
however if link points to file with most of other extensions  - say to .doc, to .xls or most of the other
it will first show the dialog asking if you want to open or to save and when you select "save" it will
open this Save As dialog as it is presented by the browser, so then user will be able to select in what folder
she wants to store the file. This behavior may vary with the browser or setup but this is how at least IE is usualy set up.
0
 
LVL 1

Author Comment

by:cano63
ID: 35192437
i don't want to place the link of the file in my application i want it to generated automaticly

I have this in my servlet

      response.setContentType("Application/csv");
      response.setContentLength(bytes.length);
                        servletOutputStream.write(bytes, 0, bytes.length);
                        servletOutputStream.flush();
                        servletOutputStream.close();

THis is suposed to show me the save as dialog, but how i pas to it my file
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35192737
I'm not sure  I understand.
Thsi is the code on the server side - it would not show anything because it is running
in closed server envirtonment. Server code usually does not have any
visual front end - whatever folder you specify in the code
or whatever folder name is passed to your server as parameter can be usesd to write the file.
But server code would normally not interact with the user.
Please, explain who you think would be browsing for the output folder to save the data
 - do you want the users of your application on the client side to do it?

In normal situation in the server you can generate file name for the server side file where you write
some stuff which your servlet generated - say the picture or table with data. Then you place the link to
thus written file on your web page.
When the user cllicks on that link and then browses to the folder on his local machine
then browser will download the contents of the
file which you prepared on the server and will download itto the local
file of the user/client which she chose.

If you mean something else, then please explain.
0
 
LVL 1

Author Comment

by:cano63
ID: 35193169
My file is already created in the C:\directory of the server, i want the user to be able to download it, but not with a link i want to call the function when he press a button.

I think i can hit a servlet and the servlet will return the save dialog
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 47

Expert Comment

by:for_yan
ID: 35193237

Well, I usually don't very much subscribe to idea of bad practices, etc.
but if there is any really bad practice - that would be to show any kind of dialog from
the servlet code.

Servlet runs on the server - most of your users woiuld not run anything on the server.
They usually access the server through browser.

If you want to do something on the server side for those who have direct access to server -
then you don't need servlet - just write java application and show JFileChooser dialog as we were talking in the very beginning.
The main purpose of the servlet is to create html page which should be
accessible to users who are not running anything directly on the server

0
 
LVL 1

Author Comment

by:cano63
ID: 35193274
The button will be in a jsp, that jsp will call the servlet
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35193335
That part is fine. And what will happen next?
0
 
LVL 1

Author Comment

by:cano63
ID: 35193358
There where i need to call my file an present the user the option to save the file, that where i,m lost
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35193370
Who will be the user - at what computer he/she will be sitting?
Will they be directly wortking on your server?
0
 
LVL 1

Author Comment

by:cano63
ID: 35193564
NO  like a web application could acced from any place the file is saved in the server
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35193601
How can she access from any place your file on the server?
To do that after you create the file on the server you return to the user the html page containing link
to that file. When user clicks that link browser opens Save As dialog.
If you open diallog sirectly in the servlet code which runs on teh server this dialog would be
seen only to those logged in deirectly to the server
0
 
LVL 1

Author Comment

by:cano63
ID: 35193642
And how i can return that link to the user using the save as dialog of the browser
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35193828

You just put link in your page like that:

1 records retrieved. Click <a href="../images/gifs/1300826636110.csv">here</a> to get the output in MSExcel format.

Open in new window


This is you path and filename on the server (but here you should be attantive it should start from server doc root) :  ../images/gifs/1300826636110.csv


The user will see ioon the page:

1 records retrieved. Click here to get the output in MSExcel format

but the word "here" would be underlined as link.
When the user clicks on it IE  will open window asking
do you want to save open, etc. Then when you say Save, IE
will open Save As dialog

Firefox has different strategy - it usually does not ask the folder,
and stores in the download folder, but perhaps it still can
be configured. Anyway this is more or less standard way users acces files
from any web sites - they would be accustomed to this method, whatever browser they use,
and you would hardly want to change this practice






0
 
LVL 1

Author Comment

by:cano63
ID: 35193854
There is a way to generate that dynamic, not putting it in the link in the jsp,

Click a button and the button do everything?
0
 
LVL 1

Accepted Solution

by:
cano63 earned 0 total points
ID: 35193947
I found it
FileInputStream fis = new FileInputStream("C://file.csv");
				 
				
				 ByteArrayOutputStream bos = new ByteArrayOutputStream();
				 byte[] buf = new byte[1024];
				 try {
				 for (int readNum; (readNum = fis.read(buf)) != -1;) {
				 bos.write(buf, 0, readNum); //no doubt here is 0
				
				 System.out.println("read " + readNum + " bytes,");
				 }
				 } catch (IOException ex) {
				
				 }
				 byte[] bytes = bos.toByteArray();

				 
				 ServletOutputStream servletOutputStream = response.getOutputStream();

					response.setContentType("Application/csv");
					response.setHeader("Content-Disposition", "attachment; filename=file.csv");
					response.setContentLength(bytes.length);
					servletOutputStream.write(bytes, 0, bytes.length);
					servletOutputStream.flush();
					servletOutputStream.close();

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35195362
Fine, if that is what you want.
I guess, now I understand what you meant: you don't want to show one more page with the link,
but want to send file back directly without the intermediate page, immediately as a result of
the work of your servlet.  Yes, I guess, that should be possible with modification of  the header.
0
 
LVL 1

Author Closing Comment

by:cano63
ID: 35308503
I found the aswer of what i was looking for.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 64
json example 39 115
wordappend challenge 8 85
Core Java. What output will be and why ? 6 58
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now