Plz help with servlet problem///// Type Exception Report Description The server
ID: 3915901 • Letter: P
Question
Plz help with servlet problem/////
Type Exception Report
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
Note The full stack trace of the root cause is available in the server logs.
----------------- I try to upload image file but the system give me this ---------------- I Bloded the line 55,66, and 105 for you ----
@WebServlet("/upload")
@MultipartConfig
public class upload extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(getServletContext().getRealPath("/images"));
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html> " +
"<html lang="en"> " +
" <head> " +
" <title>File Upload</title> " +
" <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> " +
" </head> " +
" <body> " +
" <form method="post" action="upload" enctype="multipart/form-data" > " +
" File: " +
" <input type="file" name="imageFilename" id="file" /> <br/> " +
" Destination: " +
" <input type="text" value="/tmp" name="destination"/> " +
" </br> " +
" <input type="submit" value="Upload" name="upload" id="upload" /> " +
" </form> " +
" </body> " +
"</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response); // here is line 55
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// Create path components to save the file
//final String path = request.getParameter("destination");
final String path = getServletContext().getRealPath("/images");
final Part filePart = request.getPart("imageFile");
final String fileName = getFileName(filePart); // ehre is line 66
OutputStream out = null;
InputStream filecontent = null;
final PrintWriter writer = response.getWriter();
try {
out = new FileOutputStream(new File(path + File.separator + fileName));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
writer.println("New file " + fileName + " created at " + path);
} catch (FileNotFoundException fne) {
writer.println("You either did not specify a file to upload or are "
+ "trying to upload a file to a protected or nonexistent "
+ "location.");
writer.println("<br/> ERROR: " + fne.getMessage());
} finally {
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
if (writer != null) {
writer.close();
}
}
}
private String getFileName(final Part part) {
final String partHeader = part.getHeader("content-disposition"); /// here is line 105
for (String content : part.getHeader("content-disposition").split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(
content.indexOf('=') + 1).trim().replace(""", "");
}
}
return null;
}
}
Explanation / Answer
You are getting a NULL POINTER EXCEPTION. Your are getting this error because the part object that you are passing to getFileName() as a parameter is NULL.
I see that you are getting the part value as request.getPart("imageFile");. I think this is the place where you getting NULL value, which means that your request does not any part object with the name 'imageFile'.
In order to avoid this problem, please make sure that the part you pass to getFileName() is not NULL. Or, you can include a null check too, like
if(part == null)
return null;
NOTE: If you keep on getting errors, my suggestion to you would be to set up debuggers in your IDE. If you are using Netbeans, Eclipse or Intellij, it can be easily one (you can search on web on how to set up debugger). Once done, just put debug points wherever you are getting error. This will help you a lot to tackle with errors and issues.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.