how to export a ArrayList<String> in gwt to external file?

566 views
Skip to first unread message

tong123123

unread,
Jun 14, 2012, 4:57:50 AM6/14/12
to google-we...@googlegroups.com
as captioned, assume I have an ArrayList<String>() sqlHistory in a gwt file, how to export this arraylist to an external file (prompt a save dialog and let user select where to save the file) on window platform?

Joseph Lust

unread,
Jun 14, 2012, 10:37:30 AM6/14/12
to google-we...@googlegroups.com
tong,

I have not tested this, but the following should do the trick for you.


1) Write a method to convert your sqlHistory to a string with the desired line returns (if you want each list entry on a separate row).

2) Convert it to a DataUrl for a text file with MIME "text/plain"

3) Open a new window with the URL from (2) to trigger a file download. 


Sincerely,
Joseph

Kei Kei

unread,
Jun 18, 2012, 6:13:25 AM6/18/12
to google-we...@googlegroups.com
I do not underatand how to implement step 2 and 3, so I try to use GWT-RPC to call servlet to export the data, to simplify the case, I just try to hardcode the output text first, with the following code in the RemoteServiceServlet

public void exportHistory(ArrayList<String> sqlHistory){
    this.sqlHistory = sqlHistory;
    try {
        HttpServletResponse res = this.getThreadLocalResponse();
        res.setContentType("text/plain");
        res.setHeader("Content-disposition", "attachment; filename=history.txt");
        ServletOutputStream out = res.getOutputStream();
        out.println("test1");
        out.println("test2");
        out.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

there is no exception in this segment, but it return onFailure(Throwable caught) in the asyncCallback<Void>, the caught is InvocationException.
What is the problem?
> --
> You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/JLZmg_Mnxg0J.
>
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Joseph Lust

unread,
Jun 18, 2012, 8:06:18 PM6/18/12
to google-we...@googlegroups.com
Tong,

I'm not sure whether you are saying that is it throwing an exception, or that your request is coming back with an HTTP error code:

Case 1 - Except
You are only catching IOException, which will not catch all exceptions. Try adding another catch below that for Exception or setting Eclipse to break on Exception. This should help find the error.

Case 2 - HTTP error
Let us know what the error code is by checking it either with a breakpoint or from FF/Chrome dev tools.


Sincerely,
Joseph

tong123123

unread,
Jun 18, 2012, 10:04:04 PM6/18/12
to google-we...@googlegroups.com
I have added the catch (Exception e) in ExportServiceImpl.java and found there is no any exception, but when the rpc return to client, it go to onFailure with InvocationException as shown in attachment OnFailure.png. I don't know is this HTTP error.

But what is the cause of this error?


NoException.png
OnFailure.png
Message has been deleted

tong123123

unread,
Jun 18, 2012, 11:52:17 PM6/18/12
to google-we...@googlegroups.com
Thanks for your reply.
yes, I am using rpc for the previous code.
to post a form, I remember FormPanel. but the example above is just a simplified case. in real, I need to pass a search history Map (time as the key, criteria string as the value) to servlet and use the servlet to export the Map content to a text file, Using FormPanel, how to pass the Map to servlet?
Also, seem this forum cannot edit post, so how to redact the titles of my previous attachment? delete it?

On Tuesday, June 19, 2012 11:04:46 AM UTC+8, Joseph Lust wrote:
Are you calling this from an RPC? It looks like you are (rpc.InvocationException). An RPC expects an RPC serialized response, not what it is getting. To get a file back like this, you'll need to just open it like a new browser window. 

i.e. window.open("yourServerUrl","_blank");
The above will get stopped by popup blockers. However, if you open it by posting a form to the URL with a target="_blank" tag, it should just open.

If you're not using RPC, the docs say it is a communications error, which could be many things.

Also, you might want to redact the titles of those images as they show your whole package name.




Sincerely,
Joseph

~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
Joseph R Lust
(919) 355-8785
joseph...@gmail.com

tong123123

unread,
Jun 19, 2012, 4:49:03 AM6/19/12
to google-we...@googlegroups.com
Finally I use a textbox to store the search criteria history and set its visibility to false, then submit the form (FormPanel) to server and in servlet, get the textbox value using request.getParameter("XXX");
there is just some minor unknown about the RPC

An RPC expects an RPC serialized response
What does this exactly means? the response generated with ServletOutputStream.flush is not allowed, right?
Thanks for the help!!

Joseph Lust

unread,
Jun 19, 2012, 10:10:00 PM6/19/12
to google-we...@googlegroups.com
You can delete a post using the button on the post, but you cannot edit it.

If you send your request as an RPC, then you expect to get an RPC response. If the response instead is sending different mime headers (to popup a download prompt on the browser), I'm pretty sure GWT will throw an error. That is why I was saying you would want to trigger this with a form POST to your servlet, not an RPC request, since the POST can open a download prompt.


Jospeh
Reply all
Reply to author
Forward
0 new messages