Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Upload attachments on server using JSP  RSS feed

 
Anurag Dhiman
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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you creating this folder?
 
Sam Jimzz
Greenhorn
Posts: 28
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the java code by which you are creating the folder..
 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • 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
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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: 992
5
  • 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
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!