Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying uplod in FTP but unable to get File Name it returns null

 
Ajay Nanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My project basically is to upload multiple files in ftp server and create directory as per user input and strores file in it, but when i run the project i i get the file name returns NULL, please help how i can get that


Here is my Code

FTPFileUplaod.java


Create Directory



uploadFile.jsp

 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you getting nulls? What, exactly, happens if you run this code?
 
Ajay Nanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while(iterator.hasNext()){
FileItem item = (FileItem) iterator.next();

fileName = item.getName();
System.out.println("File :" +fileName);

Here i getting the name of the fileName = value

But i add the file name in following code :

File uploadedFile = new File(fileName);
System.out.println("++++++++++++++++++++++++++++");
System.out.println("Uploaded File :" + uploadedFile.getName());

The name of uplaodedFile.getName() returns NULL.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A FileItem is not a File object (nor an actual file). Once the upload has finished, you now need to create a file to store the uploaded data permanently (assuming that that is what you want to do). Note that in order to store files in a web app you need to use absolute paths, not relative paths. Be sure to read the javadocs of FileItem.getName() about this.
 
Ajay Nanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should i do ?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Save the uploaded file - *if* that is what you want to do; you haven't told us. The Apache Commons FileUpload site has an extensive user guide and FAQ that tells you all you need to know.
 
Ajay Nanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made following changes in my code



When i run this i got the File Name but i unable store the file in to my ftp server location

I get the following exeception



Please help how can store uploaded to files to ftp server?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use a FileInputStream because the file does not exist. Please follow my earlier advice to read the user guide of the FileUpload library to learn how it actually works.
 
Ajay Nanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already read the user guide of the FileUpload library and used in my code but i am getting the result what i want

You are not getting my question.

How i can store the uploaded files in the diretcory in my ftp server
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already read the user guide of the FileUpload library and used in my code

I see nothing in the code you posted where you would handle the uploaded data. As I pointed out, using File and FileInputStream is wrong. You need to get an InputStream from the uploaded data that you can pass to the storeFile method.

A further problem (which I mentioned in an earlier post) is using FileItem.getName(). So, again: read its javadocs, particularly what it says about the behavior of some browsers. You can't use it directly as the name of the file to store via FTP - you need to process it first to make sure it is a valid file name, and not a full path.
 
Ajay Nanda
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made following changes on my code



but i getting same exception



Please provide some codes to upload multiples files from web browser to ftp server
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're still not doing this:
I see nothing in the code you posted where you would handle the uploaded data. As I pointed out, using File and FileInputStream is wrong. You need to get an InputStream from the uploaded data that you can pass to the storeFile method.


And it looks like you're not doing this, either:
A further problem (which I mentioned in an earlier post) is using FileItem.getName(). So, again: read its javadocs, particularly what it says about the behavior of some browsers. You can't use it directly as the name of the file to store via FTP - you need to process it first to make sure it is a valid file name, and not a full path.


Do you understand what both these points mean? Unless and until you do, you have little chance of making this work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic