• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 67747
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: 67747
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: 67747
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: 27807
196
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.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic