简体   繁体   English

从servlet中止上载以限制文件大小

[英]Aborting upload from a servlet to limit file size

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 the file being uploaded exceeds a limit. 为了实现这一点,我想在上传文件的大小超过限制时从服务器端中止上传过程。

Is there a way to abort an upload process from the server side without waiting the HTTP request to finish? 有没有办法在不等待HTTP请求完成的情况下从服务器端中止上传过程?

With JavaEE 6 / Servlet 3.0 the preferred way of doing that would be to use the @MultipartConfig annotation on your servlet like this: 使用JavaEE 6 / Servlet 3.0,首选方法是在servlet上使用@MultipartConfig注释 ,如下所示:

@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024, 
    maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)
public class UploadFileServiceImpl extends HttpServlet ...

You can do something like this (using the Commons library): 你可以这样做(使用Commons库):

    public class UploadFileServiceImpl extends HttpServlet
    {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
        {
            response.setContentType("text/plain");

            try
            {
                FileItem uploadItem = getFileItem(request);
                if (uploadItem == null)
                {
                        // ERROR
                }   

                // Add logic here
            }
            catch (Exception ex)
            {
                response.getWriter().write("Error: file upload failure: " + ex.getMessage());           
            }
        }

        private FileItem getFileItem(HttpServletRequest request) throws FileUploadException
        {
            DiskFileItemFactory factory = new DiskFileItemFactory();        

             // Add here your own limit         
             factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);

         ServletFileUpload upload = new ServletFileUpload(factory);

             // Add here your own limit
             upload.setSizeMax(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);


            List<?> items = upload.parseRequest(request);
            Iterator<?> it = items.iterator();
            while (it.hasNext())
            {
                FileItem item = (FileItem) it.next();
                        // Search here for file item
                if (!item.isFormField() && 
                  // Check field name to get to file item  ... 
                {
                    return item;
                }
            }

            return null;
        }
    }

You might try doing this in the doPost() method of your servlet 您可以尝试在servlet的doPost()方法中执行此操作

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. 这样您就不必等待完全处理HttpRequest并返回或向客户端显示错误消息。 HTH HTH

You can use apache commons fileupload library, this library permits to limir file size also. 你可以使用apache commons fileupload库,这个库也允许你的文件大小。

http://commons.apache.org/fileupload/ http://commons.apache.org/fileupload/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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