aspose file tools*
The moose likes Servlets and the fly likes How to download csv file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to download csv file" Watch "How to download csv file" New topic
Author

How to download csv file

Linda Thomas
Ranch Hand

Joined: Jun 21, 2004
Posts: 36
I have the code in my class that creates a .csv file on the server and now I'm trying to provide a link that will open up a save window where the user can download it to their own computer.

My link <a href="<%=URL_BASE%>/student_services/counsellors/statistics.csv">click here</a> where URL_BASE is a constant variable but it opens up an browser window with the contents of statistics.csv. If I right click and save target as it wants to save as a .html file and even though I can change the extension to .csv I'm not sure that it will save correctly.

Besides my supervisor wants a pop-up box that lets the user save to the destination they want when you click the link.

I've found some code on another site but its not clear enough to me on how to use it.

I got:
====================================================
<%@ page language="java" import="java.io.*"%>
<SCRIPT LANGUAGE="JavaScript">
function openErr(file){
//alert("file "+file);
window.document.downloadFrm.fileName.value=file;
toOpen(file);
//window.document.downloadFrm.action="/aboss/jsp/AbossFileDownload.jsp";
//window.document.downloadFrm.target="new";
//window.document.downloadFrm.submit();
}


winName="downloadWin";
var windowParams="toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=20,height=20,left=0,top=0";
function toOpen(file) {
win1 = window.open("", winName, windowParams);
window.document.downloadFrm.action="/testing/downloadTestNew.jsp";
window.document.downloadFrm.target=winName;
window.document.downloadFrm.submit();
window.document.downloadFrm.target="";
window.setTimeout('toCloseWin()', 5000);
}

function toCloseWin(){
win1.close();
}

</SCRIPT>

Then in jsp
====================================================
<%
// download
String filePath="";
String fileNameNew="statistics.csv";
filePath=URL_BASE+"/student_services/counsellors/"+fileNameNew;
response.setContentType("text/csv");
// response.setContentType("application/x-msdownload");
// or
// response.setContentType("application/unknown");
response.addHeader("Content-Disposition", "attachment; filename="+fileNameNew);
try{
File uFile= new File(filePath);
int fSize=(int)uFile.length();
FileInputStream fis = new FileInputStream(uFile);
PrintWriter pw = response.getWriter();
int c=-1;
// Loop to read and write bytes.
//pw.print("Test");

while ((c = fis.read()) != -1){
pw.print((char)c);
}
// Close output and input resources.
fis.close();
pw.flush();
pw=null;
}catch(Exception e){
}
%>

NOTE: I have no idea why there is code there that is commented out. I found response.setContentType("text/csv"); on my own and have replaced their options of
====================================================
// response.setContentType("application/x-msdownload");
// or
// response.setContentType("application/unknown");
====================================================
with it instead.

The link was
====================================================
<a target="_blank" href="<%=URL_BASE%>/student_services/counsellors/statistics.csv">
and for some reason they added
====================================================
<form name="downloadFrm" action="" method="post">
<INPUT TYPE="hidden" name="fileName">
</form>
under it.

Also included was a DownloadServlet class
====================================================
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class DownloadServlet extends HttpServlet {
private static final String basePath = "/doc";

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
String filePath = request.getPathInfo();
String filename = request.getParameter( "filename" );
if( filePath == null && filename != null ) filePath = "/" + filename;
if( filename == null ) filename = filePath;
if( filename != null ) filename = (new File(filename)).getName();

if( filePath != null ) {
InputStream in = null;
OutputStream out = null;
try {
in = getServletContext().getResourceAsStream(basePath + filePath);
if( in != null ) {
out = new BufferedOutputStream( response.getOutputStream() );
in = new BufferedInputStream( in );
String contentType = "application/unknow";
System.out.println( "contentType: " + contentType );
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
int c; while( ( c=in.read() ) != -1 ) out.write( c );
return;
}
} finally {
if( in != null ) try { in.close(); } catch( Exception e ) {}
if( out != null ) try { out.close(); } catch( Exception e ) {}
}
}
response.sendError( HttpServletResponse.SC_NOT_FOUND );
}
}

The code is very confusing and I think filename should be statistics.csv so I have replaced it but not sure if I have in the right places.

Can anyone help?
[ October 07, 2005: Message edited by: Linda Thomas ]
Lewis Lam
Greenhorn

Joined: Jan 31, 2006
Posts: 1
Dear Linda,

I'm facing the exact same problem you posted. Please let me if you have found the solution for it. Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

This would be more appropriate in the Servlets fourm. Moved.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to download csv file
 
Similar Threads
automatic download
download pop-up windows error
IllegalStateException
export to doc file
download help