Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Getting NoSuchFileException after using Resource interface

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following endpoint working fine if I run it locally like the following :



Here, I am running FileDownloadApp named application locally on eclipse which is deployed on tomcat.

-downloadFile is the name of the endpoint as shown below

-fileName.zip is the file residing inside /mnt/nfs/myfolder/prod/documents/custom_documents/JACK directory.

Basically, it's inside the JACK directory on Windows. The whole thing is meant for Unix environment but I am testing it on Windows and hence the pathname is like that.




However,when I converted it to the following - used Resource interface, I am getting the following error:




 
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in the first chunk of code you use ".../custom_documents/" + user + "/" + filename and in the second chunk you use ".../custom_documents/" + filename + "/" + user. Those are both actual code accessing the file system. The first is correct and the second is wrong, according to both the error message and the description you gave before the first chunk of code. It is very simple to fix the second chunk of code to build the file path in the correct order.

It's true that your endpoint looks like ".../filename/user" but it's unfortunate, because your correct chunk of code extracts the filename and user parts and puts them back together in the correct order.
 
Jack Tauson
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, in the first chunk of code you use ".../custom_documents/" + user + "/" + filename and in the second chunk you use ".../custom_documents/" + filename + "/" + user. Those are both actual code accessing the file system. The first is correct and the second is wrong, according to both the error message and the description you gave before the first chunk of code. It is very simple to fix the second chunk of code to build the file path in the correct order.

It's true that your endpoint looks like ".../filename/user" but it's unfortunate, because your correct chunk of code extracts the filename and user parts and puts them back together in the correct order.



Thanks. That fixed the error after making the following changes. However, it's not downloading the actual zip file. It downloaded a file of type "File" with name "JACK" and when I opened it in notepad ++ it was full of garbage characters. What is going wrong here?

 
Paul Clapham
Marshal
Posts: 26915
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack Tauson wrote:However, it's not downloading the actual zip file. It downloaded a file of type "File" with name "JACK" and when I opened it in notepad ++ it was full of garbage characters. What is going wrong here?



What's going wrong? Opening a zip file in Notepad++ and being surprised that it isn't text is one wrong thing. Just rename it to something.zip and I expect you'll be able to unzip it normally.

In your first chunk of code in the original post you had some code which assigned a file name to the downloaded data. So that worked fine. The second chunk of code doesn't have any such thing but I expect that if you changed it to send the appropriate header (maybe something like in the first chunk of code) you might get a more congenial file name attached to the downloaded data.
 
Marshal
Posts: 3727
534
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to including Content-Type: application/octet-stream header, you also need to provide a header which provides the filename; something like:
    Content-Disposition: attachment; filename="fileName.zip"
 
Ron McLeod
Marshal
Posts: 3727
534
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Details on the Content-Disposition header are described in: rfc 6266
 
Bartender
Posts: 2900
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd warn against using such code in production. I see a lot of security based issues there. For starters, you are creating a file name based off the path sent in the url parameters!! Imagine someone tries something like "../../../etc/password" or similar in {user}. I would strongly suggest validations against such inputs.
Next, this code does nothing:

I also see missing validations for checking if a file actually exists at that location and is user readable. While Files.readAllBytes seems awesome, your server is open to a DOS attack once the file size reaches a few gigs.
 
Jack Tauson
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:In addition to including Content-Type: application/octet-stream header, you also need to provide a header which provides the filename; something like:
    Content-Disposition: attachment; filename="fileName.zip"



Thanks. In my first chunk of code,I forgot to include the following ( I have edited my original post) for everyone's reference:



I wonder how do I include above stuff in my second chunk of code?

 
Saloon Keeper
Posts: 24591
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a cleaner way to do things:


Note that this code is OS-independent. Doesn't care if your filesystems use slashes, backslashes or even ">" (like the old PrimeOS OS did).

Also you can make a webapp easier to test if you obtain the filename path for documentFolder via JNDI as webapp environment object. That way you can define it at deployment time and use exactly the same code in Windows using "D:\Data\Archives", on a local Linux machine as "/home/myself/testdata/archives" and in production as "/mnt/nfs/myfolder/prod/documents/custom_documents". You won't even have to rebuild the WAR, since the values are set at deplyment time, not build time.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic