45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:上传与下载JSF文件的方法技巧

上传与下载JSF文件的方法技巧

2016-08-27 13:25:25 来源:www.45fan.com 【

上传与下载JSF文件的方法技巧

JSF文件上传与下载
--使用myfaces

<script type="text/javascript"><!-- google_ad_client = "pub-3804432954034391"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; google_ad_channel = ""; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

一、依赖的库
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://www.45fan.com/dnjc/68440.html
Tags: 文件 上传 JSF
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部