简体   繁体   中英

Can't get parameters via POST request

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")){

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,

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.
}

There are two possible chances

  • The first thing is that u might have not sending the data to the servlet for thumbnail filed
  • if the request.getParameter() method returns the null value the single & will never work , because the returned value is null and you are trying to perform the action .equals so the obvious thing is it throws an nullpointer Exception where if the string is null it doesn't allow you to perform all the actions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM