This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Uploading file in a folder under context root problem

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have been doing a file uploading task in which I got error when I try to upload one or more files under a folder under context root.

Can anyone give me the solution?

Best Regards,

Ramesh Kangamuthu
[ April 30, 2008: Message edited by: Ramesh kangamuthu ]
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh kangamuthu:
in which I got error when I try to upload



Carnac the Magnificent is unable to divine the nature of your error. Perhaps you'd better post it.
 
Ramesh kangamuthu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bear,

I have imported the neccessary libraries like apache's file upload and io libraries. No error on that.This is the code for the upload program.

boolean isMultipart=ServletFileUpload.isMultipartContent(request);

if(isMultipart==true){

FileItemFactory diskFactory=new DiskFileItemFactory();

ServletFileUpload uploadHandler=new ServletFileUpload(diskFactory);

try{
uploadHandler.setSizeMax(-1);
parameterList=uploadHandler.parseRequest(request);
Iterator fileIterator=parameterList.iterator();


//Process the uploaded items
while(fileIterator.hasNext()){
FileItem file=(FileItem)fileIterator.next();
String fileName="";
if(!(file.isFormField())){
String filePath=file.getName();
File resourceFile=new File(filePath);
fileName=resourceFile.getName();
File uploadedLocation=new
File(context.getRealPath("/")
+"/uploadedfiles/images"+"/"+fileName;

file.write(uploadedLocation);
}
}

}catch(Exception ex){
ex.printStackTrace();
}

Actually the context.getRealPath("/") returns like

d:\kramesh\EProductCatalogue\build\web\

and the folder I mentioned has uploadedfiles/images "/" character.

If I give "\\" to represent "\" in the directory structure file is not uploaded. If I try this program like

File uploadedLocation=new File(context.getRealPath("/")+fileName);

file.write(uploadedLocation); It works fine and files are loaded.

I hope you give me the solution.

Best Regards,

Ramesh Kangamuthu
 
Bear Bibeault
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and many people that might be able to help you will just move along. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Bear Bibeault
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh kangamuthu:
I hope you give me the solution.

To what? You still haven't told us what the problem is.
 
Ramesh kangamuthu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bear,

I have imported the neccessary libraries like apache's file upload and io libraries. No error on that.This is the code for the upload program.

Actually the context.getRealPath("/") returns like

d:\kramesh\EProductCatalogue\build\web\

and the folder I mentioned has uploadedfiles/images "/" character.

If I give "\\" to represent "\" in the directory structure file is not uploaded. If I try this program like

File uploadedLocation=new File(context.getRealPath("/")+fileName);

file.write(uploadedLocation); It works fine and files are loaded.

I hope you give me the solution.

Best Regards,

Ramesh Kangamuthu
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh kangamuthu:

If I give "\\" to represent "\" in the directory structure file is not uploaded.



What do you mean by 'give'?
Are you passing "\\" to the getRealPath method as an argument?
 
Ramesh kangamuthu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

My problem is How I can upload a file inside a folder under the context root using



If I use to upload without a sub directory under the context root like




, the file was uploaded. fine. but If I include a direcotory under context root the file is not uploaded without the notification of error.

Best Regards,

Ramesh Kangamuthu
 
Ramesh kangamuthu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

I meant "\\" as the character reference for "\" character.

I meant "\\" for "\" in the file path structure not in context.getRealPath() method. I used "\\" in sub directory structure like this


String fileName="image1.jpg";
and put it as

[ April 30, 2008: Message edited by: Ramesh kangamuthu ]
 
Saloon Keeper
Posts: 24869
174
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
Helpful hint #1: Use "/" instead of "\" as a path element separator. Java knows what to do, it's more OS portable, and you don't have to worry about doubling up on "/", like "\\", since the forward-slash character doesn't have a meta-meaning to Java.

Helpful hint #2: Don't try and write to the web application context. Depending on the server and the options, there might not actually be a directory to write to. For example, if it's an unexploded WAR, you'd be talking about elements inside the WAR file itself, and WARfiles are read-only.

For uploads and other writeable files, I recommend that you define the target directory as a resource in the web.xml file (you can use JNDI to get its value). That way you can change the directory location without recompiling code. In Tomcat you can even override the location at deployment time.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramesh, does the subdirectory exist in the file system?

I'm really just guessing here because we haven't seen a stack trace.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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