Spring MVC文件上传控制器-我希望在上传开始后立即调用该控制器 [英] Spring MVC file upload controller - I'd like the controller to be called as soon as the upload starts

查看:147
本文介绍了Spring MVC文件上传控制器-我希望在上传开始后立即调用该控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用裸露的servlet的doPost,当文件上传开始时,立即调用doPost.然后,我可以使用公用FileItemIterator从请求对象流式传输文件.

Using a naked servlet's doPost, when a file upload starts, doPost is immediately called. I can then stream the files from the request object using the commons FileItemIterator.

使用Spring MVC,直到之后文件全部被服务器接收,我似乎无法启动控制器方法,这是不理想的.

Using Spring MVC, I can't seem to get the controller method to fire until after the file(s) have all been received by the server, which is not ideal.

我希望我的servlet/控制器方法能够处理尽可能多的文件,并在上传中断的情况下执行一些回滚操作.我目前无法使用Spring MVC做到这一点.

I want my servlet/controller method to process as many files as it can and perform some rollback operations if an upload is interrupted. I can't do that with Spring MVC currently.

public void doPost(HttpServletRequest request, HttpServletResponse res){
//I can immediately stream the response here 
}

vs.

@RequestMapping(value="/uploadFiles", method= RequestMethod.POST)
public @ResponseBody String addFiles(ContentManagerTicket ticket, HttpServletRequest request){
//I can't do anything until the files are received - whether i use a HttpServletRequset or MultiPartFile
}

有什么想法吗?谢谢!

推荐答案

您要使用流文件上传,但是在使用 MultipartFile 可用作方法参数,并且要使其正常工作,它必须对控制器可用.

You want streaming file uploads however when using Spring’s multipart (file upload) support it uses the classic approach. This basically means that all multipart parts of a request are parsed before the request is actually handed down to the controller. This is needed because a MultipartFile can be used as method argument and for this to work it needs to be available to the controller.

如果要处理流文件上传,则必须禁用Spring的多部分支持,并执行

If you want to handle streaming file uploads you will have to disable Spring's multipart support and do the parsing yourself in the controller, the same way you would do in a servlet.

@Controller
public class FileUploadController {

    @RequestMapping("/upload")
    public void upload(HttpServletRequest request) {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
            // Inform user about invalid request
        }

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();

        // Parse the request
        FileItemIterator iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String name = item.getFieldName();
            InputStream stream = item.openStream();
            if (item.isFormField()) {
                System.out.println("Form field " + name + " with value "+ Streams.asString(stream) + " detected.");
            } else {
                System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
            // Process the input stream
            ...
            }
        }
    }
}

另请参见如何上传文件使用通用文件上传流API Apache通用文件上传流API"

这篇关于Spring MVC文件上传控制器-我希望在上传开始后立即调用该控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆