我正在使用带有倍数和 fileUploadListener 方法的 primefaces fileUpload。每次上传的每个文件都会调用监听器,我想将每个文件存储在 arrayList 中,并在最后一个上传后循环遍历列表并将它们存储在数据库中。
我的托管 bean 是 viewScoped,是否可以使用静态 arrayList 来存储上传,或者是否有更好的方法来处理这个问题?
小面
<p:fieldset legend="Info">
<p:selectOneRadio id="newold" value="#{newmailer.selectedCompStatus}">
<f:selectItem itemLabel="Existing Company" itemValue="exist" />
<f:selectItem itemLabel="New Company" itemValue="new" />
<p:ajax listener="#{newmailer.setComp}" event="valueChange" update="main" execute="@all" />
</p:selectOneRadio>
<p:panelGrid columns="2" styleClass="Grid" style="margin-bottom:10px" cellpadding="5" rendered="#{newmailer.exist}">
<h:outputLabel value="Company" id="Company" />
<p:selectOneMenu value="#{newmailer.selectedComp}" id="companies" label="Company">
<f:selectItem itemLabel="Choose Company" itemValue="" />
<f:selectItems value="#{mailerInfo.companies}" var="comp" />
<p:ajax listener="#{demo.getCompanyMailer}" event="valueChange" execute="@all" />
</p:selectOneMenu>
</p:panelGrid>
<p:panelGrid id="newPanel" styleClass="Grid" columns="2" style="margin-bottom:10px" cellpadding="5" rendered="#{!newmailer.exist and newmailer.showInfo}">
<h:outputLabel value="Company" id="Company2" />
<p:inputText id="newCompany" value="#{newmailer.selectedComp}" immediate="true">
<f:ajax event="change"/>
</p:inputText>
</p:panelGrid>
<p:panelGrid styleClass="Grid" columns="2" style="margin-bottom:10px" cellpadding="5" rendered="#{newmailer.showInfo}">
<h:outputLabel value="Mailer Id" />
<p:inputText id="mailerId" value="#{newmailer.mailerId}" immediate="true">
<f:ajax event="change"/>
</p:inputText>
</p:panelGrid>
</p:fieldset>
<p:fieldset legend="Status" rendered="#{newmailer.showInfo}">
<p:selectOneRadio id="status" value="#{newmailer.status}" immediate="true">
<f:selectItem itemLabel="Active" itemValue="A" />
<f:selectItem itemLabel="Inactive" itemValue="I" />
<f:ajax event="change"/>
</p:selectOneRadio>
</p:fieldset>
<p:fieldset legend="Description" rendered="#{newmailer.showInfo}">
<p:inputTextarea rows="5" cols="30" value ="#{newmailer.desc}" counter="counter" maxlength="10"
counterTemplate="{0} characters remaining." autoResize="false" immediate="true">
<f:ajax event="change"/>
</p:inputTextarea>
</p:fieldset>
<p:fieldset legend="Load Image" rendered="#{newmailer.showInfo}">
<p:fileUpload fileUploadListener="#{newmailer.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
process="@form"
multiple="true"
/>
</p:fieldset>
<p:growl id="messages" showDetail="true"/>
</p:panelGrid>
<!-- <p:commandButton value="Submit" type="sumbit" action="#{newmailer.submit}" ajax="false"/>-->
</h:form>
豆
@ViewScoped
@ManagedBean(name="newmailer")
public class NewMailerBean implements Serializable{
private String status;
private String compStatus;
private String selectedCompStatus;
private String selectedComp;
private String mailerId;
private String desc;
private boolean exist;
private boolean showInfo;
public static Mailer mail;
public static boolean multi=false;
public ArrayList<byte []> images = new ArrayList<byte []>();
public void handleFileUpload(FileUploadEvent event) {
Mailer mail = new Mailer();
mail.setCompany(selectedComp);
mail.setDesc(desc);
mail.setMailerId(mailerId);
mail.setStatus(status);
mail.setUserId("test");
try{
InputStream inputStream = event.getFile().getInputstream();
ByteArrayOutputStream out=new ByteArrayOutputStream(1024);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
byte[] bytearray = out.toByteArray();
inputStream.close();
out.flush();
out.close();
images.add(bytearray);
mail.setImg(bytearray);
}catch(IOException e) {
e.printStackTrace();
}