回传后上传文件名消失

时间:2019-06-03 12:32:34

标签: java forms jsp file-upload attachment

我的jsp文件中有一个简单的表单,其中 input type = file 被称为“附件”,用于使用表单发送附件。

当用户向我的表单传递了错误的数据时,我将回传回表单。

问题出在我的输入type = file。回发后消失。当用户选择文件时,我想保留有关此文件的信息。

例如-用户选择了附件,但他将错误的信息传递给表单,因此他将回发到同一页面。

提交之前:

enter image description here

提交后:

enter image description here

如您所见,用户先前选择的附件消失了

问题:回发后如何保存该文件?

我的示例代码:

Jsp文件:

<tr>
     <td><span>Attachment </span></td>
     <td><input id="attachFileObj" type="file" name="attachFileObj" size="60" value="${param.attachFileObj}"  />                   
</tr>

用于发送附件的Java代码:

@Autowired
JavaMailSender javamailSender; 

    javamailSender.send(mimeMessage -> {
                MimeMessageHelper mimeMsgHelperObj = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                mimeMsgHelperObj.setTo("xxx");
                mimeMsgHelperObj.setFrom(request.getParameter("email"));
                mimeMsgHelperObj.setText("xyz");

                // Determine If There Is An File Upload. If Yes, Attach It To The Client Email
                if ((attachFileObj != null) && (attachFileObj.getSize() > 0) && (!attachFileObj.equals("")) ) {
                    System.out.println("\nAttachment Name?= " + attachFileObj.getOriginalFilename() + "\n");
                    logger.info("\nAttachment Name?= " + attachFileObj.getOriginalFilename() + "\n");
                    mimeMsgHelperObj.addAttachment(attachFileObj.getOriginalFilename(), new InputStreamSource() {
                        public InputStream getInputStream() throws IOException {
                            return attachFileObj.getInputStream();
                        }
                    });
                } else {
                    System.out.println("\nNo Attachment Is Selected By The User. Sending Text Email!\n");
                    logger.info("No Attachment Is Selected By The User. Sending Text Email!");
                }
            });

0 个答案:

没有答案