Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

No such file or directory: Image from web-page to file-system

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  Hey, everybody!

  I try to download image-file from web-page to file-system - through servlet.
  1. In jsp-page


  2. In Servlet:


     As a result in log:
get a picture successfully
stream to servlet without problem

3.A stream-logic in:


 As a result:
filePath founded
java.io.FileNotFoundException: src/main/webapp/Images/EGEsoon.jpg (No such file or directory)


  But, when such operation in "main-method" are done successfully.
Project-structure.png
[Thumbnail for Project-structure.png]
 
Rancher
Posts: 4607
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your servlet does not execute in your IDE, inside your project.
It executes inside a container (eg Tomcat or Glassfish).

That path will not exist inside the place the web app is deployed to.

I will say that that is a non standard way to stream out a file.

You'd normally loop over the input stream, reading into a byte buffer, then writing that buffer out to the output stream.  Keep looping until the number of bytes read comes back as -1.

Now, there's almost certainly a streaming way to achieve the same thing.
 
Andrey Dmitriev
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Your servlet does not execute in your IDE, inside your project.
It executes inside a container (eg Tomcat or Glassfish).

That path will not exist inside the place the web app is deployed to.



    Dave, I forgot to show, where the servlet in project-sctructure is situated.

    The extracted structure is new attachment
New_structure.png
[Thumbnail for New_structure.png]
 
Marshal
Posts: 3144
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrey Dmitriev wrote:3.A stream-logic in:



Using Java 7 Files, you can copy an input stream to a file like this:
This doesn't address your issue however - just simplifies the code.
 
Saloon Keeper
Posts: 22248
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:
This doesn't address your issue however - just simplifies the code.



Actually, what it does is make a real mess of things.

1. /src/main/webapp is the project SOURCE code, not the executable webapp. So it's a poor choice of target.

2. You should absolutely, positively never save a file in an executable webapp (WAR). Ever. For one thing, it might not work at all, since an unexploded WAR's directories cannot be opened for filesystem writes. For another, updating the WAR will nuke any files written into the previous version of the WAR, even if the copy worked in the first place. Any files uploaded to a web server should be stored in a safe place external to the webapp, and for that matter, to the webapp server.

Also, the title was FROM web-page TO file-system, so that would appear to be the wrong direction anyway.

However, a webapp server is not a file server, so all the webapp can do is export the file content. It is the web client that has to make the determination as to whether the content will be presented to the user for saving as a file, opening in an app such as Adobe Reader, or whatever.
 
Andrey Dmitriev
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Ron McLeod wrote:


2. You should absolutely, positively never save a file in an executable webapp (WAR). Ever. For one thing, it might not work at all, since an unexploded WAR's directories cannot be opened for filesystem writes. For another, updating the WAR will nuke any files written into the previous version of the WAR, even if the copy worked in the first place. Any files uploaded to a web server should be stored in a safe place external to the webapp, and for that matter, to the webapp server.

Also, the title was FROM web-page TO file-system, so that would appear to be the wrong direction anyway.

However, a webapp server is not a file server, so all the webapp can do is export the file content. It is the web client that has to make the determination as to whether the content will be presented to the user for saving as a file, opening in an app such as Adobe Reader, or whatever.



   Ok, its clearly now.

   

 
Andrey Dmitriev
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

You'd normally loop over the input stream, reading into a byte buffer, then writing that buffer out to the output stream.  Keep looping until the number of bytes read comes back as -1.



   And really I did not do that?

 
 
Tim Holloway
Saloon Keeper
Posts: 22248
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did that.

Although these days there are stream-copy methods that eliminate the need for you to code the loop yourself.

Either way works, though. The problem is, that the server cannot write files on the client.* A web server is not the same thing as a file server.

Which is just as well, since one of the files that the server could then write might be a really vicious virus.

Instead, when you have a web server, the file contents are embedded in the HttpResponse stream and the client decides what to do with that data. As I said, the client might choose to save the content in a file, but it might choose to open it in an application. The server cannot control what the client will do. Only the client can make that decision.

====
* And the client, likewise, cannot write files directly on the server.
 
Marshal
Posts: 25669
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrey Dmitriev wrote:

Dave Tolls wrote:

You'd normally loop over the input stream, reading into a byte buffer, then writing that buffer out to the output stream.  Keep looping until the number of bytes read comes back as -1.



   And really I did not do that?

 



No, you really didn't. Even if I assume that there are brackets where they appear to be missing from that code, your for-loop copies the last byte of the input data into every single byte of the output data. And you have two byte buffers, rather than one. And I don't know how you determine the size of the first one. And... I could go on but there's no point.
 
Andrey Dmitriev
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Instead, when you have a web server, the file contents are embedded in the HttpResponse stream and the client decides what to do with that data. As I said, the client might choose to save the content in a file, but it might choose to open it in an application. The server cannot control what the client will do. Only the client can make that decision.



  Thanks. And could you advice me any example of code?
 
Paul Clapham
Marshal
Posts: 25669
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrey Dmitriev wrote:

Tim Holloway wrote:

Instead, when you have a web server, the file contents are embedded in the HttpResponse stream and the client decides what to do with that data. As I said, the client might choose to save the content in a file, but it might choose to open it in an application. The server cannot control what the client will do. Only the client can make that decision.



  Thanks. And could you advice me any example of code?



You were already given an example:



Admittedly this is an example of code to copy from the servlet input stream to a path on the server. I suggest that modifying it to copy from a path on the server to the servlet output stream shouldn't be insanely difficult; why not give that a try?
 
Tim Holloway
Saloon Keeper
Posts: 22248
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may not be insanely difficult, but it challenged me, even though I knew that it was certainly something that should be supported. There's no direct function I could find for the purpose.

Here's my attempt. I have no idea whether it would work, though, and it doesn't include things like setting the Content-Type header or computing the content length:

 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack 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
    Bookmark Topic Watch Topic
  • New Topic