JSF文件上传与下载
--使用myfaces
一、依赖的库
myfaces相关以及tomahawk等
二、配置文件
修改web.xml,加入如下代码
-------------------------------------------------------
<filter>
<filter-name>extensionsFilter</filter-name>
<filter-class>
org.apache.myfaces.webapp.filter.ExtensionsFilter
</filter-class>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>100m</param-value>
</init-param>
<init-param>
<param-name>uploadThresholdSize</param-name>
<param-value>100k</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>extensionsFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
-------------------------------------------------------
修改faces-config.xml,加入如下代码
-------------------------------------------------------
<component>
<component-type>
org.apache.myfaces.HtmlInputFileUpload
</component-type>
<component-class>
org.apache.myfaces.custom.fileupload.HtmlInputFileUpload
</component-class>
</component>
<converter>
<converter-for-class>
org.apache.myfaces.custom.fileupload.UploadedFile
</converter-for-class>
<converter-class>
org.apache.myfaces.custom.fileupload.UploadedFileConverter
</converter-class>
</converter>
<render-kit>
<render-kit-id>HTML_BASIC</render-kit-id>
<!-- extended standard renderers -->
<renderer>
<component-family>javax.faces.Input</component-family>
<renderer-type>org.apache.myfaces.FileUpload</renderer-type>
<renderer-class>
org.apache.myfaces.custom.fileupload.HtmlFileUploadRenderer
</renderer-class>
</renderer>
</render-kit>
-------------------------------------------------------
三、设计上传页面
<h:form id="sendmail" enctype="multipart/form-data" >
<t:inputFileUpload id="fileupload"
value="#{oaMailMainForm.myFile}"
storage="file"
styleClass="fileUploadInput"
maxlength="200000"/>
<f:verbatim><br></f:verbatim>
<h:commandButton image="/images/mail/sendfile.gif"
action="#{oaMailMainForm.upload}" />
</h:form>
四、oaMailMainForm的实现
import org.apache.myfaces.custom.fileupload.UploadedFile;
private UploadedFile myFile;
public String upload() throws IOException {
if(myFile==null)
return null;
log.debug("upload........" + myFile.getName());
String fileName=myFile.getName(); //获得文件的全名,含路径
if(!fileName.equals("")){
File temp = new File(fileName);
fileName = temp.getName();//获得文件名
}
log.debug(fileName);
}
byte[] buffer = new byte[new Long(myFile.getSize()).intValue()];
buffer=myFile.getBytes();
//attach set it's properties
attach.setAtFile(Hibernate.createBlob(buffer));
attach.setAtSize(new Long(buffer.length));
oaMailAttachGroup.add(attach);
//then you should save oaMailAttachGroup
return null;
}
五、下载
public String download() {
OaMailAttach oaMailAttach = getOaMailAttach();
try {
FacesContext ctx = FacesContext.getCurrentInstance();
String contentType = "application/octet-stream;charset=utf-8";
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
response.setContentType(contentType);
StringBuffer contentDisposition = new StringBuffer();
contentDisposition.append("attachment;");
contentDisposition.append("filename=\"");
contentDisposition.append(oaMailAttach.getAtFilename());
contentDisposition.append("\"");
log.debug(System.getProperty("file.encoding"));
response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));
log.debug(contentDisposition.toString());
ServletOutputStream out = response.getOutputStream();
log.debug(new Long(oaMailAttach.getAtFile().length()));
byte[] bytes = new byte[0xffff];
InputStream is = oaMailAttach.getAtFile().getBinaryStream();
int b = 0;
while ((b = is.read(bytes, 0, 0xffff)) > 0) {
out.write(bytes, 0, b);
}
is.close();
out.flush();
ctx.responseComplete();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return null;
}
六、下载页面设计
<h:form id="d">
<h:commandButton value="download" action="#{fileUploadForm.download}"></h:commandButton>
</h:form>
本文转自
http://hi.baidu.com/mymzd/blog/item/6195c011c3507010b8127b74.html
分享到:
相关推荐
自己写的JSF文件上传项目,可以最大支持最大1.99G文件 需要的jar包需奥自己加 附:jar目录截图
jsf文件上传和下载例子,可以直接在eclipse下用tomcat直接运行
JSF文件上传下载完整代码....
由于JSF2.0标准实现没有提供文件上传组件,而实际应用中很多时候需要上传文件,为了方便开发,我做了一个基于JSF2.0的文件上传组件,上传使用的是Apache 的commons-fileupload组件,我已经将commons-fileupload-...
JSF 上传文件例子 英文 经测试,能行
jsf文件上传,不错的文件上传资料
jsf官方实现默认是不支持multipart/form-data类型的表单的,为了使jsf能够解析multipart/form-data表单的数据,我参考了myfaces1.1的方式,然后按照commons-fileupload1.2...fileupload1.1)开发了一个文件上传组件。
jsf结合ajax实现文件上传,值得阅读
这个主要讲jsf上传文件,因此只罗列了上传文件用到的包和标签。 Web-xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3...
一个上传文件的jsf组件
利用JSF2.0和servlet3.0做的上传的例子,并实现单个文件上传的组件,虽然有些小限制,但是对单个上传功能完全可以实现。花了一整天的时间来研究啊,不容易!顶起吧。有什么想法请发表评论
用JSF和MyFaces上传文件,需要用到很多包,用的所有包见upfile.rar。参考实例:http://www.javanb.com/j2ee/1/2959.html
教你使用JSF和MyFaces实现文件上载。
================================= JSF开发必备JAR ================================= <br>《JSF入门简单中文版》开篇提到JSF开发需要的jar,' 但是按其中提供的方法: <br>jstl.jar 与 ...
jsf实现基于ajax的上传例子
该资源能够直接对上传大文件,速度比较快。部署后即可使用。
用户可以很方便使用浏览器上传文件,但是当前的Java Web标准(如servlets, JSP, and JSF)并没有提供任何上传的功能。幸好第三方框架,比如 Apache Commons File Upload, Apache MyFaces, and Oracle ADF Faces,...
JSF工程实例源代码,包含实现文件上传与下载的全部源代码和文档,数据库使用oracle10g
JSF+Spring+JPA以我个人看来,应该说是Struts2+Spring+Hibernate的替代解决方案。 引入JPA去取代或者说包装或者说是升级Hibernate是为了符合JAVA EE的规范,达到ORM统一的结果。下次项目用EJB也好、用TOPLINK也好、...