Reputation: 549
Want to scale a module of uploading files on server. The skeleton of the project got here. Besides of pictures want to create thumbnails of them. The size of thumbnail is selected on the jsp page and sent to servlet. The problem is that I can't get that parameters via request.getParameter("name")
. Point me please, where I am fool.
JSP (I think that will be enough)
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" multiple id="file" onchange="check('file')"/><br>
<div id="set_thumbnail" style="display: none;">
<input type="checkbox" name="thumbnail" id="thumbnail" onclick="setVisible('thumbnail','sizeThumbnailInput')"/>
<label>Create thumbnail</label>
</div>
<select id="sizeThumbnailInput" name="sizeThumbnail" style="display: none">
<option value="0">SMALL(max 100x100px)</option>
<option value="1">MEDIUM(max 250x250px)</option>
<option value="2">BIG(max 620x620px)</option>
</select><br>
<input type="submit" value="upload" />
</form>
Servlet
public class FileUploadHandler extends HttpServlet {
private final String UPLOAD_DIRECTORY = "g:\\point";
private boolean thumbnail;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path1 = "";
String path2= "";
String path = "";
if (request.getParameter("thumbnail")!= null & request.getParameter("thumbnail").equals("on")){
thumbnail = true;
}
//process only if its multipart content
if(ServletFileUpload.isMultipartContent(request)){
try {
List<FileItem> multiparts = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
System.out.println(item.getFieldName());
if(!item.isFormField()){
String name = new File(item.getName()).getName();
if (item.getName().length() < 3 || item.getSize() < 3){
request.setAttribute("message", "File is wrong or not assigned.");
request.getRequestDispatcher("/index.jsp").forward(request, response);
return;
}
path1 = UPLOAD_DIRECTORY + File.separator + name;
item.write( new File(path1));
if (thumbnail){
path2 = UPLOAD_DIRECTORY + File.separator + "thumbnail_" + item.getName();
ResizePicture resPicture = new ResizePicture(path1, path2, ResizePicture.PictureTemplate.SMALL_IMAGE);
resPicture.resize();
}
path = path1 + "\n" + path2;
}
}
request.setAttribute("message", "File Uploaded Successfully\n" + path);
} catch (Exception ex) {
request.setAttribute("message", "File Upload Failed due to " + ex);
ex.printStackTrace();
}
}else{
request.setAttribute("message",
"Sorry this Servlet only handles file upload request");
}
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
java.lang.NullPointerException
is thrown on
if (request.getParameter("thumbnail")!= null & request.getParameter("thumbnail").equals("on")){
Upvotes: 0
Views: 1628
Reputation: 3195
There are two possible chances
Upvotes: 0
Reputation: 4314
Reason for NPE is already mentioned in above answer that you are using &
, Hence both conditions will be evaluated. Replacing it with &&
will solve the issue.
You can also use an alternative, to avoid NPE and != null
check,
if ( "on".equals(request.getParameter("thumbnail")) ) {
// Do the rest.
}
Upvotes: 0
Reputation: 1777
If you do condition1 & condition2
, always two conditions are evaluated (so in your example the param "thumbnail" can be null
). You have to use condition1 && condition2
since by this way condition2
will only be evaluated if condition1
is true
Regards,
Upvotes: 4