Jack Tauson

Ranch Hand
+ Follow
since Mar 27, 2014
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
1
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jack Tauson

Rob Spoor wrote:Check out ZipOutputStream. In short:
* Create a ZipOutputStream wrapped around a FileOutputStream.
* For each file:
   * Create a ZipEntry.
   * Call putNextEntry.
   * Write content to the ZipOutputStream. Don't close it!
   * Call closeEntry.
* Close the ZipOutputStream.

That "Don't close it!" can be troublesome, because it means you can't use try-with-resources, or perform any other finalization that a wrapping OutputStream may need. A simple workaround is to create a wrapper that does nothing for its close method. You can extend FilterOutputStream and override close to do nothing, or you can use commons-io's CloseShieldOutputStream.



It looks like I'm inadvertently closing ZipOutputStream in the following code below? Because, my first file is getting generated without any problem inside the zip file. However, I believe for second and third file generation, it's throwing following error:



Here is my modified code where I'm trying to generate 3 csv files in a zip file. Could you point me the mistake I'm doing in the code below?



I tried removing zos.closeEntry() from each of the try block and placed it just above zos.close() but it still didn't work. Another thing I tried was the new variable name for CSVWriter for second and third entry, still didn't work.
5 hours ago

Rob Spoor wrote: Files has method newOutputStream that you can use just like the newBufferedWriter method.



So, I didn't have to use FileOutputStream at all. The following worked for me:




Thanks Rob and others for your valuable inputs. Appreciated!
3 days ago

Rob Spoor wrote:

Jack Tauson wrote:


That creates the actual zip file, and it puts it in "the current working directory". Why don't you use the dir variable here? Files has method newOutputStream that you can use just like the newBufferedWriter method.



Ah,I see. Looks like it wasn't able to find where to create a zip file.

You mean something like this?

Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);

FileOutputStream fos = new FileOutputStream(Files.newOutputStream(dir));
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);



I am wondering how would I define the name of the zip file here? I mean previously, I had "your_files.zip" and now it has been replaced with Files.newOutputStream(dir)?

Also, I defined dir variable before FileOutputStream as I am using dir variable below.


               
               
3 days ago

Rob Spoor wrote:ZipEntry takes a String (or another ZipEntry, but let's ignore that), not a Path. You need to convert your Path to a String. For instance, to include the full path:

It's also possible you only want the file name:


As for the CSVWriter, this takes a Writer, not an OutputStream. You need to wrap it:

However, I will repeat my previous warning:

Rob Spoor wrote:Check out ZipOutputStream. In short:
   * Write content to the ZipOutputStream. Don't close it!


Closing the CSVWriter will close its nested OutputStreamWriter which will close its nested ZipOutputStream. As a result you can't write your second or third file anymore.



So, I made the following changes:



And I don't see zip file getting generated over there. What could be the reason? Other two files are getting generated in CSV format since I haven't made changes related to Zip conversion over there.
5 days ago

Paul Clapham wrote:Short answer: the CSV data should go directly into the ZIP archive as ZipEntry objects. The FileOutputStream allows the ZIP archive to be written out to the file system as a file.




So, I am trying to modify the CSVWriter's constructor in sucn a way that the CSV data should go directly into the ZIP archive as ZipEntry objects.

So I made the changes in the above code (visible after this line ResultSetMetaData rsmd = rsDemo.getMetaData(); in the code below). Right now I am just trying to put one file into ZIP archive to test but I noticed following errors:

Error on Line #173 says: The constructor ZipEntry(Path) is undefined.

Error on Line #175 says: Multiple markers at this line (one out of 2 errors is The constructor CSVWriter(ZipOutputStream) is undefined) .

Screenshot from my eclipse below:



Modified Code below:

1 week ago

Paul Clapham wrote:

Jack Tauson wrote:f latter is true, then, at what point and how should I get the file content without getting it saved in the directory in RHEL server?



But I don't understand this question at all. Your question is about how to generate a ZIP archive which contains three CSV files. Okay. Then all of a sudden there's a RHEL server and you want to get some file content from somewhere?



I believe that's my wrong imagination.  Whatever you mentioned above, "the CSV data should go directly into the ZIP archive as ZipEntry objects" is what I'm looking for. I mean, the zip file should exist inside the folder of RHEL server.
1 week ago

Rob Spoor wrote:Check out ZipOutputStream. In short:
* Create a ZipOutputStream wrapped around a FileOutputStream.
* For each file:
   * Create a ZipEntry.
   * Call putNextEntry.
   * Write content to the ZipOutputStream. Don't close it!
   * Call closeEntry.
* Close the ZipOutputStream.

That "Don't close it!" can be troublesome, because it means you can't use try-with-resources, or perform any other finalization that a wrapping OutputStream may need. A simple workaround is to create a wrapper that does nothing for its close method. You can extend FilterOutputStream and override close to do nothing, or you can use commons-io's CloseShieldOutputStream.



Thanks. I'm little bit confused as to whether I should let my current code generate the csv files inside a directory as it is currently doing OR should it directly go into the zip archive (this makes more sense to me). If latter is true, then, at what point and how should I get the file content without getting it saved in the directory in RHEL server?

Also, how FileOutputStream could help here? I'm little bit confused. Could you give some pseudo code related guidance based on my code? Thanks!
1 week ago

I have this method in my springboot application which is generating 3 CSV files (related to Employee, Customer and Building) inside custom_users directory with timestamp appended to its name as shown below.






My questions:

Is it possible to zip these three files and then upload it inside custom_users folder? So, instead of 3 files I'll have a zip file , maybe with a name like this files_unixtimestamp.zip.
What changes I should be doing? Should I use servlet here to upload the files to the server in zip format or modify the existing code? Please advise. Thanks !

The reason I want to achieve this is :

1) I am using servlet from the client side to download these files in zip format. And since I could only download one file at a time using one servlet call, my call to the servlet would look something like this :

https://myservername.com/DownloadFileFromServer/DownloadFileServlet?filename=building_custom_file_112345786.csv

So, once I've zip related thing implemented , my call would look like the following:

https://myservername.com/DownloadFileFromServer/DownloadFileServlet?filename=files_112345786.zip


2) I won't have to append timestamp in front of the name of each of the 3 csv files.







2 weeks ago
Thanks Tim and Paul for your valuable inputs. One more quick question on which option you guys think is best to follow:

Option #1: Is it a good idea to create this servlet in my spring boot app only. And if I need the same functionality, I should create it separately for other spring boot web app?

OR

Option #2: Should I consider creating it separately, generate a WAR and deploy it with my other spring boot related WARs on Tomcat?

Option #2 sounds better to me because if I have other spring boot web apps wanting to use the same functionality, I could use this separately deployed servlet to download the file from the server. However,  different spring boot webapps might be downloading file from different location so not sure if I could tweak the servlet
to refer to different location on the server or not.
2 weeks ago
Thanks for the answer. I have few more follow-up questions:

1. Pretty much exactly this. Although it can be tweaked to not hard-code the filename being copied.



Right now my user interface (which consists of Javascript, HTML,CSS) calls REStful webservices(springboot) for user related operations like  getting data etc. For this download thing, user will hit Download button which behind the scenes is going to
get the files to the user from a specific user directory.

In this scenario, should I create a webservice (define an end point in my controller; define doGet method in my DAO interface and have it implemented in the implementation class of DAO interface) along with my other web services?
I am actually, trying to imagine how user (as soon as they hit Download button) would get the file(s) in zip format after they hit Download button.



2. JavaScript isn't needed, although if your client webpage wants to be more dynamic it can request the file via an AJAX URL request.



I am wondering because user is pressing the Download button, Javascript might be needed? And would I need additional libraries lke FileSaver.js that I have used above in my  javascript code?
Sorry, I am little confused here.
2 weeks ago

Tim Holloway wrote:
Create a file-copy servlet. This is a better approach, because not only can you read files from any place on the server's filesystem that Tomcat has read-access rights to, but you can also edit those files while copying them or even create "files" from scratch to copy. Since it's a servlet, all that's required is to open the source file, copy/modify/create data as neede and output it to the HttpServletResponse output stream along with the usual headers (Content-Type, Content-Length and so on). If you're minded to, you can even pass in the source directory path via JNDI so that it's not hard-coded into the WAR.



Thanks for detailed explanation. Appreciated.

1)Could you tell me by create file-copy servlet, you are referring to something like this?

https://stackoverflow.com/questions/20342457/java-servlet-read-a-file-and-send-it-as-response

2)Also, how Javascript would be interacting with it and getting the file from the server in this scenario.

3)Would this work if the file size is huge? Maybe 500MB or more?
2 weeks ago

Tim Holloway wrote:Yes, JavaScript does have a sandbox, although I've never seen a formal list of its restrictions the way that the ill-fated Java Applet sandbox had.

Some things that applets couldn't do - which are probably also true for JavaScript:

1. Access the client's local filesystem (e.g., erase critical OS files, send a copy of private documents to Bulgaria, implant viruses).

2. Access the client's local printing subsystem (start a print job of 1 billion pages each with a single character on the page)

3. Communicate with servers other than the one that served up the applet. For example, www.botcontroller.com.

It is possible to sign Java applets - and I believe that this is also true of JavaScript - so that these constraints can be relaxed, but the process is tedious and ugly.



Thanks.

1) I am only trying to access local files system on Windows for testing (in my Scenario #1).

2) On RHEL server, I'm trying to access it from /srv/ directory. Is this also considered as local file system? If not, what could be the reason I am not getting contents of the file but empty csv files?

3) Any alternative to this you would recommend?
3 weeks ago

Al Hobbs wrote:The thread mentioning it: thread
I'm guessing the reason it works when you run it from a local html file is because the browser can see that the code is local to the computer based on the url or some other way.  When you get the html from a server it can see the url location is not local.



Thanks. Maybe I should move this thread to here :

https://coderanch.com/f/20/HTML-Pages-CSS-JavaScript

Is there a way I could do that or should I post the same question there?
3 weeks ago

Al Hobbs wrote:I don't you will be able to read the files using javascript.  There is another thread with a similar topic about read/write using javascript in a web page. For security purposes javascript doesn't have access to the local file system directly.  something like that



Ok. Thanks. How come it's reading the contents of the file when I am accessing the index.html like the following from my web browser as I discussed in my post above.

file:///C:/jack/testing/ui/static/index.html


Also, do you have the link to that discussion thread page that you mentioned? Thanks
3 weeks ago

Is the javascript that reads those files in the index.html or run from there?



Thanks. Yes, the javascript is inside index.html inside <script></script> tags. It runs from index.html only

If it's running on a client computer how is javascript that runs on their computer going to download files from the server with a local file system address?



The Scenario #1 I mentioned above is not for other users, that's just for my local testing with Eclipse and hence I am trying to figure out how can I access files for my local testing in that case.


Scenario #2 is RHEL server related.

Please let me know if I can answer more questions.
3 weeks ago