Creating and loading a CSV Java file (Servlet)

I am working on a JAVA EXTJS application in which I need to create and upload a CSV file.

When I click the button, I want the CSV file to be downloaded to the client machine.

In button listening mode, I invoke the servlet using ajax. There I create a CSV file.

I do not want the CSV file to be saved on the server. I want the file to be created dynamically with the ability to download.

I want the contents of the file to be created as a string, and then I will serve the content as a file, in which it will be displayed as the download mode in the browser. (This I achieved in another language, but not sure how to achieve it in java)

Here is my code only for creating a CSV file, but I really don't want to create or save a CSV file if I can only download the file as a CSV.

public String createCSV() { try { String filename = "c:\\test.csv"; FileWriter fw = new FileWriter(filename); fw.append("XXXX"); fw.append(','); fw.append("YYYY"); fw.append(','); fw.append("ZZZZ"); fw.append(','); fw.append("AAAA"); fw.append(','); fw.append("BBBB"); fw.append('\n'); CSVResult.close(); return "Csv file Successfully created"; } catch(Exception e) { return e.toString(); } } 

Can anyone help me on this.

thanks

+8
java csv extjs4
source share
3 answers

I got a solution and I am posting it below.

 public void doGet(HttpServletRequest request, HttpServletResponse response) { response.setContentType("text/csv"); response.setHeader("Content-Disposition", "attachment; filename=\"userDirectory.csv\""); try { OutputStream outputStream = response.getOutputStream(); String outputResult = "xxxx, yyyy, zzzz, aaaa, bbbb, ccccc, dddd, eeee, ffff, gggg\n"; outputStream.write(outputResult.getBytes()); outputStream.flush(); outputStream.close(); } catch(Exception e) { System.out.println(e.toString()); } } 

Here we do not need to save / store the file on the server.

thanks

+20
source share

First of all, you need to get the HttpServletResponse object so that you can flush the file into it.

Note. This example is what I wrote for one of my projects and it works. Powered by Java 7.

Assuming you got an HttpServletResponse, you could do something like this to transfer the file. Thus, the file will be saved in the client machine.

 public void downloadFile(HttpServletResponse response){ String sourceFile = "c:\\source.csv"; try { FileInputStream inputStream = new FileInputStream(sourceFile); String disposition = "attachment; fileName=outputfile.csv"; response.setContentType("text/csv"); response.setHeader("Content-Disposition", disposition); response.setHeader("content-Length", String.valueOf(stream(inputStream, response.getOutputStream()))); } catch (IOException e) { logger.error("Error occurred while downloading file {}",e); } } 

And the flow method should be like that.

 private long stream(InputStream input, OutputStream output) throws IOException { try (ReadableByteChannel inputChannel = Channels.newChannel(input); WritableByteChannel outputChannel = Channels.newChannel(output)) { ByteBuffer buffer = ByteBuffer.allocate(10240); long size = 0; while (inputChannel.read(buffer) != -1) { buffer.flip(); size += outputChannel.write(buffer); buffer.clear(); } return size; } } 

What is it, get the input stream from the source file and write this stream to the HttpServletResponse output stream. This should work, as it works fine for me. Hope this helps. Sorry for my bad english.

+3
source share

I would like to add something to gaurav's answer. I recently had to inject this functionality into my project and using javascript was out of the question because we had to support IE 9. What is the problem with IE 9? ( Export to CSV using jQuery and html ), see the second answer in the link.

I need an easy way to convert a ResultSet of a database query into a string that represents the same data in CSV format. For this, I used http://opencsv.sourceforge.net/ , which provided an easy way to get a string from a ResultSet, and the rest as above.,

The examples in the soruce folder of the project give good examples.

0
source share

All Articles