Record in CSV files and then Zipping it in Appengine (Java)

I am currently working on a project that runs in Java on the Google appengine.

Appengine does not allow files to be stored, so any view objects on disk cannot be used. Some of them include the File class.

I want to write data and export it to several csv files, and then pin it and allow the user to download.

How can I do this without using file classes? I'm not very good at processing files, so I hope you guys can advise me.

Thanks.

+4
source share
2 answers

If your data is not huge, the value can remain in memory and then export to CSV, and also encrypt it and load it for download, everything can be done on the fly. Caching can be performed at any of these steps, which is highly dependent on the business logic of the application.

+3
source

You can create a zip file and add it while the user uploads it. If you are using a servlet, this is rather complicated:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ..... process request // ..... then respond response.setContentType("application/zip"); response.setStatus(HttpServletResponse.SC_OK); // note : intentionally no content-length set, automatic chunked transfer if stream is larger than the internal buffer of the response ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream()); byte[] buffer = new byte[1024 * 32]; try { // case1: already have input stream, typically ByteArrayInputStream from a byte[] full of previoiusly prepared csv data InputStream in = new BufferedInputStream(getMyFirstInputStream()); try { zipOut.putNextEntry(new ZipEntry("FirstName")); int length; while((length = in.read(buffer)) != -1) { zipOut.write(buffer, 0, length); } zipOut.closeEntry(); } finally { in.close(); } // case 2: write directly to output stream, ie you have your raw data but need to create csv representation zipOut.putNextEntry(new ZipEntry("SecondName")); // example setup, key is to use the below outputstream 'zipOut' write methods Object mySerializer = new MySerializer(); // ie csv-writer Object myData = getMyData(); // the data to be processed by the serializer in order to make a csv file mySerizalier.setOutput(zipOut); // write whatever you have to the zipOut mySerializer.write(myData); zipOut.closeEntry(); // repeat for the next file.. or make for-loop } } finally { zipOut.close(); } } 

There is no reason to store your data in files unless you have memory limits. The files give you InputStream and OutputStream, both of which have equivalents in memory.

Note that creating a csv record usually means doing something like this , where you need to take a piece of data (a list of arrays or a map, whatever you do) and turn it into bytes []. Add bytes [] to OutputStream using a tool such as DataOutputStream (create your own if you want) or OutputStreamWriter.

+9
source

Source: https://habr.com/ru/post/1341457/


All Articles