• Post Reply Bookmark Topic Watch Topic
  • New Topic

Uploading file in a folder under context root problem  RSS feed

 
Ramesh kangamuthu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!