2012-02-04 91 views
1

大家好我開發在動態的基於Web的應用程序,我使用的struts-2文件標記取文件[圖像文件] ..這裏是代碼存儲圖像到MySQL數據庫使用stuts 2和休眠

<s:file name="userImage" label="User Image" /> 

現在我的POJO類代碼是在這裏....

public class FileUpload { 
private File userImage; 

//private String userImageContentType; 

private String userImageFileName; 

    //private HttpServletRequest servletRequest; 

public File getUserImage() { 
    return userImage; 
} 

public void setUserImage(File userImage) { 
    this.userImage = userImage; 
} 

public String getUserImageFileName() { 
    return userImageFileName; 
} 

public void setUserImageFileName(String userImageFileName) { 
    this.userImageFileName = userImageFileName; 
} 
} 

現在在行動類的這樣的代碼.....

public class FileUploadAction extends ActionSupport implements ModelDriven{ 

    private FileUpload user = new FileUpload(); 
    public FileUploadAction() 
    { 
    } 

    public Object getModel() { 
       return user; 
    } 

    public FileUpload getUser() { 
     return user; 
    } 

    public void setUser(FileUpload user) { 
     this.user = user; 
    } 
public String execute() throws IOException 
{ 
try{ 
     String filePath = ServletActionContext.getServletContext().getRealPath("/"); 
     System.out.println("Server path:" + filePath); 
     File fileToCreate = new File(filePath, user.getUserImageFileName()); 
     FileUtils.copyFile(user.getUserImage(), fileToCreate); 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
     //addActionError(e.getMessage()); 
     return INPUT; 
    } 
    return SUCCESS; 
} 
} 

現在我想存儲圖像文件數據庫使用休眠和我創建的數據庫屬性圖像作爲BLOB數據類型所以任何人可以幫助我什麼是在執行方法寫的代碼,使圖像存儲在數據庫和還如何檢索和JSP顯示它.....提前

+1

你真的應該在這裏發帖之前做了谷歌搜索。 http://snehaprashant.blogspot.com/2008/08/how-to-store-and-retrieve-blob-object.html – 2012-02-04 07:34:50

+0

在上面的鏈接,他們只顯示如何加載/檢索.doc文件,但我的圖像文件...... – user1184777 2012-02-04 09:09:01

回答

0

謝謝,我想改正在你的代碼。當你使用模型驅動的方法時,不需要模型對象的getter和setter。刪除這些。

public FileUpload getUser() { 
    return user; 
} 

public void setUser(FileUpload user) { 
    this.user = user; 
} 

只有以下就足夠了

public Object getModel() { 
      return user; 
}