• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Upload attachments on server using JSP

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Frienz!
I have created a jsp page for uploading file. I am creating an upload folder on server at runtime. The problem is when I am restarting the server (tomcat) the upload folder automatically removed. I want to use the same folder which was created on first time on file upload.
Please share some ideas how can I prevent the removal of the folder from server on restart.







Thanks & regards
 
Sheriff
Posts: 67268
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you creating this folder?
 
Greenhorn
Posts: 28
Mac Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the java code by which you are creating the folder..
 
Rancher
Posts: 1039
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you create it in the container's temporary directory?
Or in a directory in the subtree of the one where the application was installed (the war file unpacked)?
 
Bear Bibeault
Sheriff
Posts: 67268
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:Or in a directory in the subtree of the one where the application was installed (the war file unpacked)?



(This is what I'm suspecting.)
 
Anurag Dhiman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your comments!

So in this way I am creating the folder under webapps directory on Tomcat (C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\MyProject\uploads)

String root = getServletContext().getRealPath("/"); //Getting Root Path
File path = new File(root + "/uploads"); // Adding uploads to path
System.out.println("Path value is" +path);
if (!path.exists()) {
boolean status = path.mkdirs();

}

I have three modules for uploading the files So I am using same code in each module. I want to use same folder for all modules
 
Bear Bibeault
Sheriff
Posts: 67268
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we suspected: if you upload the files inside they web app, how do you expect to be able to redeploy?
 
Anurag Dhiman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are write Bibeault. Every time when I redeploy the project It will create new directory.
So you mean I have to change this path to somewhere else write?
 
Bear Bibeault
Sheriff
Posts: 67268
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be better.
 
Anurag Dhiman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you suggests a better area (whether c drive or somewhere else ) where this folder would be persisted.?
 
Ivan Jozsef Balazs
Rancher
Posts: 1039
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually any folder outside the deployed web apps and outside the temporary folders?

Make it a parameter to the application.
 
Anurag Dhiman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!
Once again Thanks to all..!!
 
Anurag Dhiman
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone
I have created a separate folder inside tomcat directory(c\apache\tomcat\uploads) for uploading files. Its working fine. But I am able to fetch the file from there(like some jpg file I want to display from that folder).
I am giving the same path in <img src="c\apache\tomcat\uploads"> but its not working. Please give some idea how to solve it
 
Bear Bibeault
Sheriff
Posts: 67268
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to write a servlet that can stream the file as its response. This has been discussed many times in these forums so you can search for previous discussions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!