简体   繁体   English

将Mulitpart文件从Controller发送到JSP(春季)

[英]Send Mulitpart file from Controller to JSP (Spring)

My requirement is to upload a JPEG/PDF file & save it as BLOB . 我的要求是上传JPEG / PDF文件并将其另存为BLOB We have done it. 我们已经做到了。 But if server side error occurs which redirects to JSP page, I will get all data (eg input fields, drop downs, checkbox, etc.) except file. 但是,如果发生服务器端错误并重定向到JSP页面,我将获得除文件之外的所有数据(例如,输入字段,下拉列表,复选框等)。 Then again I need to choose a file. 然后再次需要选择一个文件。 Is there any way to preserve a file or send a file from controller to JSP. 有什么方法可以保留文件或将文件从控制器发送到JSP。

No it is not possible. 不,这是不可能的。

The simplest workaround is to keep the uploaded file in session so that you can recover it during the next form submission. 最简单的解决方法是将上载的文件保留在会话中,以便您可以在下次提交表单时将其恢复。 Take care to users working with several tabs/windows: use a session key that clearly identifies the form on which the user is working. 请注意使用多个选项卡/窗口的用户:使用会话密钥可以清楚地标识用户正在使用的表单。 You could for example generate a unique identifier that you then store in a hidden field of the form. 例如,您可以生成一个唯一的标识符,然后将其存储在表单的隐藏字段中。

To be able to download it again, you would need to provide second mapping that retrieves the file from the session. 为了能够再次下载它,您需要提供第二个映射来从会话中检索文件。

    MultipartFile inputFile = fileUploadBean.getFile();
    HttpSession session = request.getSession();
    if(!(inputFile.isEmpty())) {
        session.setAttribute("inputFile", inputFile);
    }
    logger.info("inputFile : " + session.getAttribute("inputFile"));
    if(inputFile.isEmpty() && session.getAttribute("inputFile")!=null) {
        inputFile = (MultipartFile)session.getAttribute("inputFile");
    }

This is what I did. 这就是我所做的。

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

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