• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Getting Null Pointer Exception when i use request.getPart(inputname)

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In Servlet, when a request is made to post a file to the server, the enctype attribute of the Form in html will be specified as "multipart/form-data" and the input we give will be as type "file". And we will be dealing the input as Part(the propertiy of a request Object). In my one of code i upload a file through form to FTP server. But it shows null pointer exception. Below is the code for form and the servlet response(i have mentioned where the null pointer exception is thrown) in the Post method.

Form code:

<form method="POST" action="upload" enctype="multipart/form-data">
<p>Browse for a file to upload: </p>
<input type="file" name="file" id="file">
<br /><br />
<input type="submit" value="Upload It Now">
</form>

Servlet code:

Part filePart = request.getPart("file"); //returns null
System.out.println(filePart);
String fileName = getFileName(filePart); //this is another method which is given below
System.out.println(fileName);

below is the getFileName(Part part) method

private String getFileName(Part part) {
String partHeader = part.getHeader("Content-Disposition");
for (String content : partHeader.split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
}

I am not sure as why null is return in the first method request.getPart()

Any help would be great and helpful

Thanks in advance

Regards
Ramki S
 
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In my one of code i upload a file through form to FTP server.



Are we talking about an HTTP or an FTP here?
 
Ramki Sukumaran
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. am talking about uploading a file to an FTP server.
 
Ramki Sukumaran
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why no reply
 
Marshal
Posts: 25805
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it would help if you mentioned exactly which "other open source project" you were asking about.
 
Paul Clapham
Marshal
Posts: 25805
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would also help if you explained the relationship between the servlet you're asking about and the FTP server to which some upload is somehow being done.
 
Ramki Sukumaran
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for your reply.

Yes, I could tell you the relationship between servlet and the FTP server. The FTP server to which i want to upload is something that i have maintaining in my local machine. That is i have created FTP server locally using IIS. Now the FTP server folder is said to be inside the "E:\Inetpub\ftproot". Below is the code that contain the implementation inside the doPost method of a servlet to upload a FILE to FTP server.

Part filePart = request.getPart("file"); //returns null
System.out.println(filePart);
String fileName = getFileName(filePart); //returns Null Pointer Exception
System.out.println(fileName);


InputStream filecontent = null;

//out = new FileOutputStream(new File(path + File.separator + fileName));
filecontent = filePart.getInputStream();


FTPClient ftp = null;

String hostName = "localhost";
String username = "ramki";
String password = "partha";

ftp = new FTPClient();
ftp.connect(hostName);
ftp.login(username, password);

ftp.setFileType(FTP.BINARY_FILE_TYPE);

ftp.changeWorkingDirectory("/Sukumaran/Ramki");

int reply = ftp.getReplyCode();
System.out.println("Received Reply from FTP Connection:" + reply);

if(FTPReply.isPositiveCompletion(reply))
System.out.println("Connected Success");

ftp.storeFile(fileName,filecontent);

System.out.println("SUCCESS");


}
private String getFileName(Part part) {
String partHeader = part.getHeader("Content-Disposition");
for (String content : partHeader.split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
}

In that i have used the implementations from Apache Commons net API. In the above code i have used the FTP server host as "localhost and username and password to connect to FTP server.

Please help me as i need it urgently.

Regards
Ramki S
 
Ramki Sukumaran
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I resolved the Exception by using "@MultipartConfig". This annotation must be used to indicate that servlet or a form is recieving the Multi Part request from the client.

Thanks any way..
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic