我正在寻找通过拖放功能上传文件的解决方案。我将 JSF 1.2 与 JBoss 5.0 一起使用。如果任何 RichFaces 版本上都有解决方案,请分享。
谢谢
我想你是新来的richfaces
。Richfaces 为其 Showcase 提供了如何执行此操作的源代码。
结帐 Richfaces Showcase,在这里。
.html 文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich">
<ui:composition>
<h:outputStylesheet>
.top {
vertical-align: top;
}
.info {
height: 202px;
overflow: auto;
}
</h:outputStylesheet>
<h:form>
<h:panelGrid columns="2" columnClasses="top,top">
<rich:fileUpload fileUploadListener="#{fileUploadBean.listener}" id="upload" acceptedTypes="jpg, gif, png, bmp"
ontyperejected="alert('Only JPG, GIF, PNG and BMP files are accepted');" maxFilesQuantity="5">
<a4j:ajax event="uploadcomplete" execute="@none" render="info" />
</rich:fileUpload>
<h:panelGroup id="info" layout="block">
<rich:panel bodyClass="info">
<f:facet name="header">
<h:outputText value="Uploaded Files Info" />
</f:facet>
<h:outputText value="No files currently uploaded" rendered="#{fileUploadBean.size==0}" />
<rich:dataGrid columns="1" value="#{fileUploadBean.files}" var="file" rowKeyVar="row">
<rich:panel bodyClass="rich-laguna-panel-no-header">
<h:panelGrid columns="2">
<a4j:mediaOutput element="img" mimeType="image/jpeg" createContent="#{fileUploadBean.paint}"
value="#{row}" style="width:100px; height:100px;" cacheable="false">
<f:param value="#{fileUploadBean.timeStamp}" name="time" />
</a4j:mediaOutput>
<h:panelGrid columns="2">
<h:outputText value="File Name:" />
<h:outputText value="#{file.name}" />
<h:outputText value="File Length(bytes):" />
<h:outputText value="#{file.length}" />
</h:panelGrid>
</h:panelGrid>
</rich:panel>
</rich:dataGrid>
</rich:panel>
<br />
<a4j:commandButton action="#{fileUploadBean.clearUploadData}" render="info, upload" value="Clear Uploaded Data"
rendered="#{fileUploadBean.size>0}" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</ui:composition>
</html>
Java 文件:
package org.richfaces.demo.fileupload;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;
/**
* @author Ilya Shaikovsky
*/
@ManagedBean
@SessionScoped
public class FileUploadBean implements Serializable {
private ArrayList<UploadedImage> files = new ArrayList<UploadedImage>();
public void paint(OutputStream stream, Object object) throws IOException {
stream.write(getFiles().get((Integer) object).getData());
stream.close();
}
public void listener(FileUploadEvent event) throws Exception {
UploadedFile item = event.getUploadedFile();
UploadedImage file = new UploadedImage();
file.setLength(item.getData().length);
file.setName(item.getName());
file.setData(item.getData());
files.add(file);
}
public String clearUploadData() {
files.clear();
return null;
}
public int getSize() {
if (getFiles().size() > 0) {
return getFiles().size();
} else {
return 0;
}
}
public long getTimeStamp() {
return System.currentTimeMillis();
}
public ArrayList<UploadedImage> getFiles() {
return files;
}
public void setFiles(ArrayList<UploadedImage> files) {
this.files = files;
}
}
我提供了UploadedImage类的代码,它与FileUploadBean类在同一个包中。 获取 UploadedImage 类代码的链接
package org.richfaces.demo.fileupload;
import java.io.Serializable;
public class UploadedImage implements Serializable {
private String name;
private String mime;
private long length;
private byte[] data;
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public String getName() {
return name;
}
public void setName(String name) {
int extDot = name.lastIndexOf('.');
if (extDot > 0) {
String extension = name.substring(extDot + 1);
if ("bmp".equals(extension)) {
mime = "image/bmp";
} else if ("jpg".equals(extension)) {
mime = "image/jpeg";
} else if ("gif".equals(extension)) {
mime = "image/gif";
} else if ("png".equals(extension)) {
mime = "image/png";
} else {
mime = "image/unknown";
}
}
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public String getMime() {
return mime;
}
}
我希望我能帮助那些想要使用“rich:fileUpload”组件的人