如何用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
原创粉丝点击