Aborting upload from a servlet to limit file size

前端 未结 4 1656
余生分开走
余生分开走 2020-12-18 07:03

I\'d like to limit the size of the file that can be uploaded to an application. To achieve this, I\'d like to abort the upload process from the server side when the size of

4条回答
  •  臣服心动
    2020-12-18 07:49

    You might try doing this in the doPost() method of your servlet

    multi = new MultipartRequest(request, dirName, FILE_SIZE_LIMIT); 
    
    if(submitButton.equals(multi.getParameter("Submit")))
    {
        out.println("Files:");
        Enumeration files = multi.getFileNames();
        while (files.hasMoreElements()) {
        String name = (String)files.nextElement();
        String filename = multi.getFilesystemName(name);
        String type = multi.getContentType(name);
        File f = multi.getFile(name);
        if (f.length() > FILE_SIZE_LIMIT)
        {
            //show error message or
            //return;
            return;
        }
    }
    

    This way you don't have to wait to completely process your HttpRequest and can return or show an error message back to the client side. HTH

提交回复
热议问题