posts - 5,  comments - 7,  trackbacks - 0

后台实现类:

 

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.context.FacesContext;

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


public class Fileupload
{
    
private UploadedFile _upFile;
    
private String _name = "";

   
public UploadedFile getUpFile()
    
{
        
return _upFile;
    }


 

public void setUpFile(UploadedFile upFile)    
    
{
        _upFile 
= upFile;
    }


    
public String getName()
    
{
        
return _name;
    }


    
public void setName(String name)
    
{
        _name 
= name;
    }


    
public String upload() throws IOException
    
{
        FacesContext facesContext 
= FacesContext.getCurrentInstance();
        
int start=_upFile.getName().lastIndexOf("\\")==-1?_upFile.getName().lastIndexOf("\\"):_upFile.getName().lastIndexOf("\\")+1;
        String file
=_upFile.getName().substring(start,_upFile.getName().length());
        String path
=Fileupload.class.getResource("/").getPath().substring(1,Fileupload.class.getResource("/").getPath().length());
        path
=path.replace("%20"" ")+file;
  
try {
   FileOutputStream keyFOS 
= new FileOutputStream(path);
   keyFOS.write(_upFile.getBytes());
   keyFOS.close();
  }
 catch (Exception e) {
   System.out.println(
"lalala");
  }

  
        facesContext.getExternalContext().getApplicationMap().put(
"fileupload_bytes", _upFile.getBytes());
        facesContext.getExternalContext().getApplicationMap().put(
"fileupload_type", _upFile.getContentType());
        facesContext.getExternalContext().getApplicationMap().put(
"fileupload_name", _upFile.getName());
        
return path;
    }


public boolean isUploaded()
    
{
        FacesContext facesContext 
= FacesContext.getCurrentInstance();
        
return facesContext.getExternalContext().getApplicationMap().get("fileupload_bytes")!=null;
    }

}


 

前台实现:Myfaces

 

<t:inputFileUpload id="fileupload"
                               value
="#{action$fixing.myFile}"         
                               storage
="file"
                               styleClass
="fileUploadInput"
                               maxlength
="200000"/>
      
<h:commandButton value="#{prompt['Submit']}"
       action
="#{action$fixing.sel}" />

 

 

后台管理Bean:

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

 

public class FixingAction{

private UploadedFile myFile;

 

public UploadedFile getMyFile() {
  return myFile;
 }

 

 

public void setMyFile(UploadedFile myFile) {
  this.myFile = myFile;
 }


    public String sel(){
       Fileupload file=new Fileupload();

String path=file.setUpFile(this.myFile);

System.out.println("文件存放路径:"+path);

return null;

  }
}
posted on 2008-11-27 11:17 Vincent-chen 阅读(253) 评论(0)  编辑  收藏 所属分类: JSF

只有注册用户登录后才能发表评论。


网站导航: