0

I have to create a CSV file on a nightly process and avail those files to client as an http download.

I have created a CSV with the help PLSQL block as:

DECLARE F UTL_FILE.FILE_TYPE; CURSOR C1 IS SELECT emp_id, name FROM employee; C1_R C1%ROWTYPE; BEGIN F := UTL_FILE.FOPEN('EXPORT_DIR','employee.csv','w',32767); FOR C1_R IN C1 LOOP UTL_FILE.PUT(F,C1_R.emp_id); UTL_FILE.PUT(F,','||C1_R.name); UTL_FILE.NEW_LINE(F); END LOOP; UTL_FILE.FCLOSE(F); END; / 

this will create a CSV file "employee.csv" on the database server. Now I have to avail this file to the clients as a HTTP downloadable file. How to do that ?

3
  • where is your files store is it in table ? Commented Jan 6, 2014 at 13:42
  • no, its on server side on, stored on the disk with a specific name. Commented Jan 7, 2014 at 6:28
  • ok.. than passed that file path to below example. This will prompt user to save or open file. Commented Jan 7, 2014 at 6:57

2 Answers 2

1

You can provide one Servlet to download your file as a csv format.

DoanloadServlet.java :

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ //set content type to csv and response as attachment response.setHeader("Content-Type", "text/csv"); response.setHeader("Content-Disposition", "attachment; filename=Myfile.csv"); OutputStream out = response.getOutputStream(); //read your file from database in outputstream FileInputStream in = new FileInputStream(my_file); byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) > 0){ out.write(buffer, 0, length); } in.close(); out.flush(); } catch(Exception e) { //Exception handling } } 

Give user a link and call this servelet with required parameter to find file from database.
You link look like this
http://serveraddress.com/DoanloadServlet?fileName=name_of_file

Sign up to request clarification or add additional context in comments.

Comments

0

I found it easy to copy the generated file web server. we can just use SCP or FTP to the generated file on webserver. e.g.

scp /mnt/csv_dir/feed.csv root@server:/home/stackoverflow/www/csv 

Add this command in a nightly process to run The above command will move the feed.csv from csv_dir to csv folder of web server.

now we will be able to access the file using the web url.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.