我有一个具有两个用于保存附件的富文本字段的表单和一个包含两个文件下载控件(每个字段一个)和两个按钮的XPage.

这些按钮调用Java类中的一个方法,该方法创建一个包含相关字段中的文件的压缩文件.在浏览器中为用户下载该压缩文件.

这一切都是完美的--除了如果你下载了任何一个压缩文件,你不能立即下载另一个.在第二次点击按钮做任何事情之前,大约有15-20秒的延迟.

XPage:-

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.data>
        <xp:dominoDocument var="document1" formName="Document"></xp:dominoDocument>
    </xp:this.data>
    
    <xp:panel tagName="div"
        style="margin-left:auto; margin-right:auto; width:50%">

        <xp:br></xp:br>

        <xp:fileDownload rows="30" id="fileDownload1"
            displayLastModified="false" value="#{document1.Body1}">
        </xp:fileDownload>
        
        <xp:button value="Download Zipfile 1" id="button1">
            <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
                <xp:this.script><![CDATA[console.log("Button1 click")]]></xp:this.script>
                <xp:this.action><![CDATA[#{javascript:uk.co.mp.DownloadAllAttachments.downloadFromField(context.getUrlParameter("documentId"), "Body1");
}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>

        <xp:br></xp:br>

        <xp:fileDownload rows="30" id="fileDownload2"
            displayLastModified="false" value="#{document1.Body2}">
        </xp:fileDownload>

        
        <xp:button value="Download Zipfile 2" id="button2">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="complete">
                <xp:this.script><![CDATA[console.log("Button2 click")]]></xp:this.script>
                <xp:this.action><![CDATA[#{javascript:uk.co.mp.DownloadAllAttachments.downloadFromField(context.getUrlParameter("documentId"), "Body2");
}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>

    </xp:panel>

</xp:view>

Java类:-

package uk.co.mp;

import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import lotus.domino.Document;
import lotus.domino.EmbeddedObject;
import lotus.domino.RichTextItem;

import com.ibm.xsp.designer.context.XSPContext;
import com.ibm.xsp.model.domino.DominoUtils;

//
import lotus.domino.Session;
import lotus.domino.Database;
import com.ibm.xsp.extlib.util.ExtLibUtil;

public class DownloadAllAttachments {
    

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void downloadFromField(String documentUNID, String fieldName) {

        // Initialize global XPage objects
        FacesContext facesContext = FacesContext.getCurrentInstance();
        XSPContext context = XSPContext.getXSPContext(facesContext);
        UIViewRoot view = facesContext.getViewRoot();
        Map viewScope = view.getViewMap();

        try {

            if (documentUNID == null || documentUNID.trim().equals("")) {
                viewScope.put("errorString", "URL parameter \"documentUNID\" or \"u\" is required.");
                view.setRendered(true);
                return;
            }

            Session s = ExtLibUtil.getCurrentSessionAsSigner();
            Database db = s.getCurrentDatabase();
            
            // Get and validate the document from which attachments would be zipped
            Document downloadDocument = null;
            downloadDocument = db.getDocumentByUNID(documentUNID);

            // Get and validate zip file name from URL
            String zipFileName = fieldName + ".zip";

            boolean bAnyAttachments = false;
            EmbeddedObject embeddedObj = null;
            Vector attachments = null;
            
            // ...check for attachments in the field supplied and collect the names of the attachments
            RichTextItem rtitem = (RichTextItem)downloadDocument.getFirstItem(fieldName);
            Vector vec = rtitem.getEmbeddedObjects();
            attachments = new Vector(vec.size());
            
            if (!vec.isEmpty()) {
                Iterator it = vec.iterator();

                while(it.hasNext()){
                    embeddedObj = (EmbeddedObject)it.next();
                    if (embeddedObj != null) {
                        attachments.add(embeddedObj.getName());
                        bAnyAttachments = true;
                    }

                }
            }

            
            //By this point we should have a list of the attachment names we want to include in the zip file            

            //If we have not found any attachments then return a message
            if (!bAnyAttachments) {
                viewScope.put("errorString", "<center><h3>No attachments found in the Document.</h3></center>");
                return;
            }

            // Set response header values
            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", -1);
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename=" + zipFileName);

            // Get stream related objects
            OutputStream outStream = response.getOutputStream();
            ZipOutputStream zipOutStream = new ZipOutputStream(outStream);
            BufferedInputStream bufferInStream = null;

            // Get all attachment names and loop through all of them adding to zip
            for (int i = 0; i < attachments.size(); i++) {
                embeddedObj = downloadDocument.getAttachment(attachments.get(i).toString());
                if (embeddedObj != null) {
                    bufferInStream = new BufferedInputStream(embeddedObj.getInputStream());
                    int bufferLength = bufferInStream.available();
                    byte data[] = new byte[bufferLength];
                    bufferInStream.read(data, 0, bufferLength); // Read the attachment data
                    ZipEntry entry = new ZipEntry(embeddedObj.getName());
                    zipOutStream.putNextEntry(entry);
                    zipOutStream.write(data); // Write attachment into Zip file
                    bufferInStream.close();
                    embeddedObj.recycle();
                }
            }


            // Clean up and close objects
            downloadDocument.recycle();
            zipOutStream.flush();
            zipOutStream.close();
            outStream.flush();
            outStream.close();
            facesContext.responseComplete();

        } catch (Exception e) {
            viewScope.put("errorString", "Error while zipping attachments.<br>" + e.toString());
            e.printStackTrace();
        }
    }
    
    
}

在那15-20秒内,有没有什么东西可以阻止第二次点击按钮?非常感谢您的建议

推荐答案

try 在您的按钮的客户端JS事件中添加XSP.allowSubmit().

<xp:button value="Download Zipfile 1" id="button1">
    <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
        <xp:this.script><![CDATA[console.log("Button1 click")]]></xp:this.script>
        <xp:this.action><![CDATA[#{javascript:uk.co.mp.DownloadAllAttachments.downloadFromField(context.getUrlParameter("documentId"), "Body1");
}]]></xp:this.action>
        <xp:this.script><![CDATA[XSP.allowSubmit()]]></xp:this.script>
    </xp:eventHandler>
</xp:button>

Java相关问答推荐

Java字符串常数池困惑

具有额外列的Hibert多对多关系在添加关系时返回NonUniqueHealthExcellent

JPackaged应用程序启动MSI调试,然后启动System. exit()

Mat. n_Delete()和Mat. n_release的区别

CAMEL 4中的SAXParseException

现场观看Android Studio中的变化

对运行在GraalVM-21上的JavaFX应用程序使用分代ZGC会警告不支持JVMCI,为什么?

使用UTC时区将startDatetime转换为本地时间

Arrays.hashcode(int[])为不同的元素提供相同的散列

如何使用Criteria Builder处理一对多关系中的空值?

错误:不兼容的类型:Double不能转换为Float

FETCH类型设置为LAZY,但它仍会发送第二个请求

Java在操作多个属性和锁定锁对象时使用同步和易失性

AWS Java SDK v2.x中没有setObjectAcl方法

在Spring Boot JPA for MySQL中为我的所有类创建Bean时出错?

使用for循环时出现堆栈溢出错误,但如果使用if块执行相同的操作,则不会产生错误

JavaFX复杂项目体系 struct

持续时间--为什么在秒为负数的情况下还要做额外的工作?

获取所有可以处理Invent.ACTION_MEDIA_BUTTON Android 13 API33的Android包

为什么Spring要更改Java版本配置以及如何正确设置?