ITeye博客

<rich:fileUpload>批量上传

feidaodalian · 2009-08-10 18:54 · richfaces · Markdown

public class FileUploadBean {
    public void uploadListener(UploadEvent event) { 
        //获取传过来的items,3.2.2之前版本不支持getUploadItems方法
        List itemList = event.getUploadItems();
        for(int i=0 ; i<itemList.size() ; i++){
            try {            
                UploadItem item = (UploadItem) itemList.get(i);
                File file = new File("d:\\"+UUID.randomUUID().toString()+".jpg");
                //大小不能超过4M
                if(item.getFileSize() <= 4000000){
                    FileInputStream fis = new FileInputStream(item.getFile());   
                    FileOutputStream out = new FileOutputStream(file);   
                    int bytes = 0;   
                    byte[] bteFile = new byte[1024];   
                    while ((bytes = fis.read(bteFile)) != -1) {   
                        out.write(bteFile, 0, bytes);   
                    }
                    fis.close();
                    out.close();                    
                }
            } catch (Exception e) {  

            }             
        }
    } 
}
<%@page pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <f:view>
            <h:form>
<h:panelGroup>   
<h:outputText value="上传头像"></h:outputText>   
<rich:spacer height="3" />
   <rich:fileUpload   fileUploadListener="#{fileUploadBean.uploadListener}" 
                   maxFilesQuantity="5" 
                   autoclear="false" 
                   addControlLabel="添加图片" 
                   uploadControlLabel="上传" 
                   clearAllControlLabel="清除所有" 
                   clearControlLabel="清除" 
                   cancelEntryControlLabel="取消" 
                   stopControlLabel="停止" 
                   stopEntryControlLabel="停止" noDuplicate="true" 
                   listHeight="400" 
                   listWidth="515" 
                   style="height:100;" 
                   id="upload" 
                   acceptedTypes="jpg, gif, png, bmp">        
               </rich:fileUpload>   

       <h:outputText value="支持 JPG、JPEG、GIF、BMP 和 PNG 文件格式,最大4M。">   
                </h:outputText>   
           </h:panelGroup>
            </h:form>
        </f:view>
    </body>
</html>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。