RichFaces 파일 업로드

2835 단어 Richfacesext
Java 코드:
public String uploadListener(UploadEvent event) {
            String url;
            UploadItem item = event.getUploadItem();
            String fileName = item.getFileName();
            System.out.println("fileName:"+fileName);
            int a = fileName.lastIndexOf(".");
            //             
            String ext = fileName.substring(a+1);
            System.out.println("ext:"+ext);
            SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            //             
            String fileNewName=matter.format(System.currentTimeMillis())+"."+ext;
            System.out.println("fileNewname:"+fileNewName);
            try {
            String filepath = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/")+ "upload"+"/";
            System.out.println("filePath:"+filepath);
            File file = new File(filepath, fileName);
            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);
            }
        } catch (Exception e) {
        }
        url = "upload" + "/" +fileNewName;
        System.out.println("url:"+url);
        return url;
    }

Java 코드:
<h:form>
<h:panelGroup>
<h:outputText value=" "></h:outputText>
<rich:spacer height="3" />
   <rich:fileUpload   fileUploadListener="#{MyBean.uploadListener}"
                   maxFilesQuantity="5"
                   autoclear="false"
                   addControlLabel=" "
                   uploadControlLabel=" "
                   clearAllControlLabel=" "
                   clearControlLabel=" "
                   cancelEntryControlLabel=" "
                   stopControlLabel=" "
                   stopEntryControlLabel=" " noDuplicate="true"
                   id="upload"
                   >
            <h:outputText value="{_KB}KB from {_KB}KB uploaded --- {mm}:{ss}" />
               </rich:fileUpload>
           </h:panelGroup>
            </h:form>

좋은 웹페이지 즐겨찾기