如何用JSF技术上传文件(PrimeFaces)
来源:互联网 发布:第一源码 编辑:程序博客网 时间:2024/05/24 04:00
UploadImg_student 托管Bean
package pojo.student;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.faces.application.FacesMessage;import javax.faces.context.ExternalContext;import javax.faces.context.FacesContext;import javax.servlet.http.HttpSession;import org.primefaces.model.UploadedFile;import dao.DAOFactory;import dao.student.StudentDAO;import pojo.Student;public class UploadImg_student {FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext extContext =facesContext.getExternalContext(); HttpSession session =(HttpSession)extContext.getSession(true); private DAOFactory dao = DAOFactory.getDAOFactory(DAOFactory.MYSQL); private StudentDAO studentdao = dao.getStudentDAO(); private UploadedFile file;public UploadedFile getFile() {return file;}public void setFile(UploadedFile file) {this.file = file;} public void submit() throws IOException{ Student stu = (Student) this.session.getAttribute("student"); String stu_name = stu.getStu_name(); String rootPath = session.getServletContext().getRealPath("/upload/"); //得到服务器相对路径 File outputPath = new File(rootPath); File outputFile = new File(rootPath, file.getFileName()); //根据文件名创建路径if (!outputPath.exists()) { outputPath.mkdir(); //如果upload文件夹不存在就创建} byte[] buffer = file.getContents(); //得到上传文件的内容FileOutputStream outStream = new FileOutputStream(outputFile);outStream.write(buffer);outStream.close();String imgURL = "/JSFDemo" + "/upload/" + file.getFileName();Pattern pat = Pattern.compile("(\\.|\\/)(gif|jpe?g|png)"); Matcher mat = pat.matcher(file.getFileName()); boolean rs = mat.find(); if(!rs) {facesContext.addMessage(null, new FacesMessage("文件格式不正确"));return;}studentdao.updateIconByName(stu_name, imgURL);stu.setStu_icon(imgURL); System.out.println(rootPath); System.out.println("Uploaded file name : " + file.getFileName()); System.out.println(outputFile); } }uploadimg.xhtml
<?xml version="1.0" encoding="UTF-8"?><!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:ui="http://java.sun.com/jsf/facelets"xmlns:h="http://java.sun.com/jsf/html"xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"xml:lang="en" lang="en"><ui:composition template="/templates/student.xhtml"><ui:define name="title">上传头像</ui:define><ui:define name="css"><link href="#{request.contextPath}/css/student/uploadImg.css" rel="stylesheet" type="text/css" /></ui:define><ui:define name="content"> <div id="content" > <h:outputText value="您的头像:" /> <img src="#{sessionScope.student.stu_icon }" width="160" height="160"/> <h:form enctype="multipart/form-data"><p:fileUpload value="#{uploadImg_student.file}" mode="simple"/><p:commandButton value="提交" ajax="false" actionListener="#{uploadImg_student.submit}"/><h:messages /></h:form> </div></ui:define></ui:composition></html>
在faces-config.xml中注册Bean
<managed-bean><managed-bean-name>uploadImg_student</managed-bean-name><managed-bean-class>pojo.student.UploadImg_student</managed-bean-class><managed-bean-scope>request</managed-bean-scope></managed-bean>
0 0
- 如何用JSF技术上传文件(PrimeFaces)
- 如何用jspSmartUpload上传文件:)
- JSF 2.0(基于Primefaces 3.4.2)文件上传与下载的总结
- PrimeFaces+JSF
- 如何用seam实现文件上传
- asp.net 如何用webclient上传文件
- 如何用FileItem来实现文件上传
- 如何用FTP软件上传网站文件
- JSF-PrimeFaces实现图片的上传、下载、删除、预览
- Primefaces 3.4.2 "文件上传"的总结
- PrimeFaces <p:fileUpload>上传文件 中文乱码
- 程序员如何用技术变现(上)
- JSF + Primefaces表单验证
- jsf+primefaces问题汇总
- JSF+primefaces开发总结
- JSF:DataModel 中如何用单选
- 如何用Struts实现动态(不定数量)多个文件上传
- 如何用Struts实现动态(不定数量)多个文件上传?
- Android 常用 Intent
- IOS开发数据库篇—SQLite模糊查询
- 第1周-项目1-旱冰场造价
- 细节杂记 移除字符串中的字符 remove indexof 字符串大小比较 compare
- Client/Sever模式的通信——Java
- 如何用JSF技术上传文件(PrimeFaces)
- ViewPager的使用(上)
- Android常用Intent
- 树莓派插入DS18B20 无法读取数据解决办法
- 文件创建失败问题
- 仿商业网站——商品评分效果实现【提高篇】
- 第一章27页第5题
- Cocoa and Cocoa Touch 一个是用于开发OS X的app 一个是用于开发iPhone的app
- ARM工控核心板在尿液监测仪中的应用