a.jsf文件上传:
实现jsf应用的文件上传功能,可以使用MyFaces文件上传组件
依赖的库有 commons-io.jar,myfaces-api.jar,myfaces-impl.jar,tomahawk.jar
web.xml:
<filter>
<filter-name>MyfacesExtensionsFilter</filter-name>
<filter-class>
org.apache.myfaces.webapp.filter.ExtensionsFilter
</filter-class>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>10m</param-value>
</init-param>
<init-param>
<param-name>uploadThresholdSize</param-name>
<param-value>50k</param-value>
</init-param>
<init-param>
<param-name>uploadRepositoryPath</param-name>
<param-value>../</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyfacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>MyfacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>
////////////////////////////////
其他的jsf 和 myFaces 配置就不写在这里了
//////////////////////////////////
entity:student
public class Student{
public Student(){
}
private byte[] stuFileData;
private String stuFileName;
public void setStuFuleData(byte[] stuFileData){
this.stuFileData = stuFileData;
}
public byte[] getStuFileData(){
return this.stuFileData;
}
public void setStuFileName(String stuFileName){
this.stuFileName = stuFileName;
}
public String getStuFileName(){
return this.stuFileName;
}
}
受管bean:StudentAction
import org.apache.myfaces.custom.fileupload.UploadedFile;
public StudentAction{
private UploadedFile _upFile;
public String addStudent(){
// 获得文件的全名,含路径
String fileName = _upFile.getName();
if (!fileName.equals("")) {
File temp = new File(fileName);
// 获得文件名
fileName = temp.getName();
}
byte[] buffer = new byte[new Long(_upFile.getSize()).intValue()];
try {
buffer = _upFile.getBytes();
} catch (IOException e) {
e.printStackTrace();
}
Student student = new Student();
student.setStuDataName(fileName);
student.setStuDataFileData(buffer);
//studentDAO.save(student);
return null;
}
public String down(){
try {
//先检索student
Student student = stdentDAO.getStudent();
System.err.println("...................in...............");
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
String contentType = "application/x-msdownload;charset=utf-8";
response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment; filename=\"" +student.getStuFileName+"\"");
ServletOutputStream out = response.getOutputStream();
if(null!=crFile)
out.write(crFile);
out.flush();
ctx.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public UploadedFile get_upFile() {
return _upFile;
}
public void set_upFile(UploadedFile file) {
_upFile = file;
}
}
jsp:
首先引入标签
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
<h:form id="from" enctype="multipart/form-data">
<t:inputFileUpload id="fileupload" value="#{StudentAction._upFile}"/>
<h:commandButton action="StudentAction.addStudent" value="保存"/>
<h:commandButton action="StudentAction.down" value="下载附件"/>
</h:form>
以上程序未经调试,运行时可能会出现错误,不过用 myFaces 实现 jsf 的文件上传功能
需要注意的要点和关键性代码都已将完成了。
b.seam 的文件上传
seam应用的文件上传也可以使用 myFaces,使用方法和上面相同。
不过seam自身也提供了附件上传的组件功能,而且实现起来更简单:
xhtml文件:
首先引入标签
xmlns:s="http://jboss.com/products/seam/taglib"
<h:form id="from" enctype="multipart/form-data">
<s:fileUpload id="myFile" fileName="#{student.stuFileName}" data="#{student.stuFileData}"/>
<a4j:commandButton value="保存" action="#{StudentAction.addStuent}"/>
<a4j:commandButton value="下载附件" action="#{StudentAction.down}"/>
</h:form>
受管bean:StudentAction
import org.apache.myfaces.custom.fileupload.UploadedFile;
public StudentAction{
private Student student;
@In(request=false,create=true)
@Out(request=false)
public String addStudent(){
//studentDAO.save(student);
return null;
}
public String down(){
try {
//先检索student
Student student = stdentDAO.getStudent();
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
String contentType = "application/x-msdownload;charset=utf-8";
response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment; filename=\"" +student.getStuFileName+"\"");
ServletOutputStream out = response.getOutputStream();
if(null!=crFile)
out.write(crFile);
out.flush();
ctx.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
entity:student
@Name("student")
public class Student{
public Student(){
}
private byte[] stuFileData;
private String stuFileName;
public void setStuFuleData(byte[] stuFileData){
this.stuFileData = stuFileData;
}
@Column(name="StuFileData",length=2147483647)
@Basic(fetch=FetchType.LAZY)
@Lob
public byte[] getStuFileData(){
return this.stuFileData;
}
public void setStuFileName(String stuFileName){
this.stuFileName = stuFileName;
}
@Column(name = "StuFileName", length = 100)
@Length(max = 100)
public String getStuFileName(){
return this.stuFileName;
}
}
特别注意 jsp form 的提交方式
<h:form id="from" enctype="multipart/form-data">
否则fileData不能传递到受管 bean