Seam圖片上傳&加載

兩種方法:1.保存到數據庫后從數據庫讀取加載

               2.保存到服務器本地后讀取加載

 

兩種graphicImage組件:jsf的和seam的

 

第二種方法頁面只能用seam的graphicImage組件,且不支持gif格式

 

第一種方法保存到數據庫里的是url地址

第二種方法保存到數據庫里的是byte數組

 

 Entity:

@SuppressWarnings("serial")
@Entity
public class UserPicture implements Serializable
{
	private Integer id;
    private String name;
    private String url;
    private byte image[];
    
    
    @Id
    @GeneratedValue
    public Integer getId() {
    	return id;
    }
    public void setId(Integer id) {
    	this.id = id;
    }
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public byte[] getImage() {
		return image;
	}
	public void setImage(byte[] image) {
		this.image = image;
	}
}

 

Action:

@SuppressWarnings("serial")
@Name("pictureHome")
public class PictureHome extends EntityHome<UserPicture>
{
	@DataModel
	List<UserPicture> userPictureList;
	
	@SuppressWarnings("unchecked")
	@Factory("userPictureList")
	public void getUpList() {
		userPictureList = this.getEntityManager().createQuery("from UserPicture").getResultList();
	}
    
	public void save(ByteArrayInputStream data,String fileName,String userName) {
		
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
		String fileUrl = "C://userPicture/" + userName + "." + fileType;
		File file = new File(fileUrl);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    	try {
			FileOutputStream fos = new FileOutputStream(file);
			Integer ch = 0;
			while ((ch=data.read())!=-1) {
	    		fos.write(ch);
	    		baos.write(ch);
	    	}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.instance.setUrl(fileUrl);
		this.instance.setImage(baos.toByteArray());
    	this.persist();
    }
}

 頁面:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:s="http://jboss.com/products/seam/taglib"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml">

	<ui:define name="body">

		<rich:panel>
			<h:form>
				<f:facet name="header">pictureList</f:facet>

				<div class="results"><h:outputText value="No picture exists"
					rendered="#{empty userPictureList}" /> <h:dataTable id="upList"
					var="p" value="#{userPictureList}"
					rendered="#{not empty userPictureList}">
					<h:column>
						<f:facet name="header">Id</f:facet>
						<h:outputText value="#{p.id}" />
					</h:column>
					<h:column>
						<f:facet name="header">Name</f:facet>
						<h:outputText value="#{p.name}" />
					</h:column>
					<h:column>
						<f:facet name="header">picture</f:facet>
						<h:graphicImage url="#{p.url}" />
					</h:column>
					<h:column>
						<f:facet name="header">picture</f:facet>
						<s:graphicImage value="#{p.image}"></s:graphicImage>
					</h:column>

				</h:dataTable></div>
			</h:form>
		</rich:panel>

		<div class="actionButtons"><s:button id="done"
			value="Create picture" view="/picture.xhtml" /></div>

	</ui:define>

</ui:composition>

 

你可能感兴趣的:(UI,F#,JSF,Richfaces,seam)