jsf上传图片

总结下图片上传:所用的开发工具版本如下:jsf1.2+ejb3.0+jboss4.2.3

准备工作:导入有关三个文件传输的jar包:commons-io-1.3.2.jar  commons-fileupload-1.2.1.jar   tomahawk12-1.1.14.jar,注意此时不要添加myfaces-extension.jar包,否则会冲突。

在web.xml写好配置:在<web-app>添加一个filter结点:

 <filter>
<filter-name>extensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<description>Set the size limit for uploaded files.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB</description>
<param-name>uploadMaxFileSize</param-name>
<param-value>10m</param-value>
</init-param>
<init-param>
<description>Set the threshold size - files
below this limit are stored in memory, files above
this limit are stored on disk.

Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB</description>
<param-name>uploadThresholdSize</param-name>
<param-value>1k</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>extensionsFilter</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>


第一步:在jsp页面开始出导入标签:<%@ taglib prefix="t" uri="http://myfaces.apache.org/tomahawk" %>。

并在form表单设定enctype="multipart/form-data",因为上传文件必须设定mime的值为multipart/form-data。比如我的jsp页面是这样写的:<h:form id="inputnewgoods" enctype="multipart/form-data">.关于mime和enctype的选择可以参考:为什么上传文件的表单里要加个属性enctype

然后是表单里面写上上传文件的组建:<t:inputFileUpload id="uploadfile" value="#{backingbean.upFile }"  
    required="true" onchange="previewImage(this)" storage="file">,关于t:inputFileUpload的其他属性及含义可以参考官网介绍t:inputFileUpload:http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_inputFileUpload.html 

注意上面的onchange="previewImage(this)"中previewImage(this)是文件预览功能,用到了html5的文件API。

提交表单:<h:commandButton id="submitbutton" value="提交" action="#{backingbean.upload}"/>

第二步:建一个backing bean 我设置的backingbean的范围是session,网上有些博客说要设置成request,但是我的表单中还有级联菜单,实现级联菜单必须要用到session范围内的backingbean否则会出现异常。

代码大致如下:

......

import org.apache.myfaces.custom.fileupload.UploadedFile;

......

public class backingbean{

...

private UploadedFile upFile;

...

...

public UploadedFile getUpFile() {
return upFile;
}
public void setUpFile(UploadedFile upFile) {
this.upFile = upFile;
}

...

...

public String upload(){

 try {
           InputStream in = new BufferedInputStream(upFile.getInputStream());
           try {                
               byte[] buffer = new byte[(int)upFile.getSize()];                             
               //取得文件名
                        String name = upFile.getName();

                      //存路径
                String trace = "D:\\images\\" +name;//本地测试
    
               FileOutputStream fileOutputStream = 
                   new FileOutputStream(trace); //这里可以把上传的文件写服务器目录,或者数据库中 ,或者赋值给name用于文件名显示


               while (in.read(buffer) > 0) {
                   fileOutputStream.write(buffer);
               }
           } catch (Exception x) {
               System.out.print("出错啦!!!");
               FacesMessage message = 
                   new FacesMessage(FacesMessage.SEVERITY_FATAL, 
                                    x.getClass().getName(), x.getMessage());
               FacesContext.getCurrentInstance().addMessage(null, message);                
           }
           finally {
               in.close();
               FacesContext facesContext = FacesContext.getCurrentInstance();
               facesContext.getExternalContext().getApplicationMap().put("fileupload_bytes",                                                                          upFile.getBytes());
               facesContext.getExternalContext().getApplicationMap().put("fileupload_type", 
                                                                        upFile.getContentType());
           }
           System.out.println("End");


       } catch (Exception x) {
        System.out.print("出错啦!!!22222");
           FacesMessage message = 
               new FacesMessage(FacesMessage.SEVERITY_FATAL, 
                                x.getClass().getName(), x.getMessage());
           FacesContext.getCurrentInstance().addMessage(null, message);
       }

              return null;

}

}

最后注意在face-config里面设置这个backingbean。


你可能感兴趣的:(jsf上传图片)