当前位置:编程学习 > JAVA >>

文件上传下载 源码

public class FileUploadBean {
    private UploadedFile file;

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public void upload() {

        BufferedInputStream inStream = null;
        FileOutputStream fileOutputStream = null;
        if (file != null) {
            try {
                inStream = new BufferedInputStream(file.getInputstream());
                String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/upload");
                File superFile = new File(path);
                if (!superFile.exists()) {
                    superFile.mkdirs();
                }
                File descFile = new File(superFile, file.getFileName());
                fileOutputStream = new FileOutputStream(descFile);
                byte[] buf = new byte[1024];
                int num;
                while ((num = inStream.read(buf)) != -1) {
                    fileOutputStream.write(buf, 0, num);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inStream != null) {
                    try {
                        inStream.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }

        FacesMessage msg = new FacesMessage("Successful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

}

<-------------------->
 <h:form enctype="multipart/form-data">
            <p:messages showDetail="true"/>
            <p:fileUpload value="#{fileUploadBean.file}"   mode="simple"/>
            <br/><br/>
            <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadBean.upload}"/>
            <br/><br/><br/>
            <p:commandLink value="Download" ajax="false">
                <p:fileDownload value="#{fileDownloadBean.file}" />
            </p:commandLink>
        </h:form> 源代码 primefaces  JSF  fileupload primfaces JSF --------------------编程问答-------------------- 如何才能支持 所有浏览器啊 --------------------编程问答-------------------- 上传的代码都运行在服务端,和浏览器的兼容性没有关系吧 --------------------编程问答--------------------
引用 2 楼 s478853630 的回复:
上传的代码都运行在服务端,和浏览器的兼容性没有关系吧

不 运行之后 有的浏览器不 支持 --------------------编程问答-------------------- 怎么个不支持,哪些浏览器不支持?用struts的插件可以否? --------------------编程问答-------------------- 我感觉,浏览器不支持的不是你的上传,而是你的自定义标签 --------------------编程问答--------------------
引用 4 楼 muscle1990 的回复:
怎么个不支持,哪些浏览器不支持?用struts的插件可以否?
  ie8  ie7 不支持 你有没有不用框架的上传下载代码呀 参考一下 --------------------编程问答--------------------
引用 5 楼 chenxinhui741 的回复:
我感觉,浏览器不支持的不是你的上传,而是你的自定义标签
  对 就是primefaces 的组件才发生这状况 你有没有不用框架支持各ie的上传下载呀 --------------------编程问答--------------------
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,