2012-02-02 47 views
0

嗨,我建立一個動態的Web項目,其中歡迎頁有struts2文件標記現在我想存儲指定的文件到MySQL數據庫會有人幫助我...如何將圖像從瀏覽器存儲到mysql數據庫使用struts 2和休眠

在此先感謝。

這是我開發的代碼,但它需要文件參數靜態意味着手動我指定路徑。但它應該採取的路徑從Struts的文件標籤看到Java類U將得到它..

public class FileUploadACtion 
{ 

    public String execute() throws IOException 
{ 
System.out.println("Hibernate save image into database"); 
    Session session = HibernateUtil.getSessionFactory().openSession(); 

    session.beginTransaction(); 

    //save image into database 
    File file = new File("C:\\mavan-hibernate-image-mysql.gif"); 
    byte[] bFile = new byte[(int) file.length()]; 

    try { 
    FileInputStream fileInputStream = new FileInputStream(file); 
    //convert file into array of bytes 
    fileInputStream.read(bFile); 
    fileInputStream.close(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 

    FileUpload tfile = new FileUpload(); 
    avatar.setImage(bFile); 

    session.save(tfile); 

    //Get image from database 
    FileUpload tfile2 =   (FileUpload)session.get(FileUpload.class,FileUpload.getAvatarId()); 
    byte[] bAvatar = avatar2.getImage(); 
    try{ 
     FileOutputStream fos = new FileOutputStream("C:\\test.gif"); 
     fos.write(bAvatar); 
     fos.close(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

    session.getTransaction().commit(); 
} 

}

+0

你的問題是不明確的all.add更多細節,使一些人可以幫你。 – 2012-02-02 10:20:00

回答

0

你應該在表中的圖像形式存儲的BLOB類型。假設你有一個Person類,存儲在DB中的人的image。如果你想映射這個,只需在你的人POJO中添加一個屬性來保存圖像。

@Column(name="image") 
@Blob 
private Blob image; 

當你顯示它,將其轉換爲byte[]和表演。

private byte[] toByteArray(Blob fromImageBlob) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    try { 
     return toByteArrayImpl(fromImageBlob, baos); 
    } catch (Exception e) { 
    } 
    return null; 
    } 



private byte[] toByteArrayImpl(Blob fromImageBlob, 
     ByteArrayOutputStream baos) throws SQLException, IOException { 
    byte buf[] = new byte[4000]; 
    int dataSize; 
    InputStream is = fromImageBlob.getBinaryStream(); 

    try { 
     while((dataSize = is.read(buf)) != -1) { 
     baos.write(buf, 0, dataSize); 
     }  
    } finally { 
     if(is != null) { 
     is.close(); 
     } 
    } 
    return baos.toByteArray(); 
    } 

你可以看到下面的例子來了解更多。

  1. http://i-proving.com/space/Technologies/Hibernate/Blobs+and+Hibernate
  2. http://snehaprashant.blogspot.com/2008/08/how-to-store-and-retrieve-blob-object.html
  3. http://viralpatel.net/blogs/2011/01/tutorial-save-get-blob-object-spring-3-mvc-hibernate.html
0

那麼你不需要手動做到這一點,當你將在文件中使用的Struts2上傳的文件,它的構建上裝載攔截器會做主要令人振奮。 您只需在動作類中指定一些屬性,以便Framework將動作類中的require數據注入,並且可以執行其他工作。

這裏是你必須do.In你的JSP頁面,您需要使用<s:file>標籤

<s:form action="doUpload" method="post" enctype="multipart/form-data"> 
    <s:file name="upload" label="File"/> 
    <s:submit/> 
</s:form> 

的文件上傳攔截器將使用setter注入到上傳的文件和相關數據插入到你的Action類的東西。對於命名上傳表單字段中,您將提供在下面的例子中所示的三個setter方法: 而在你的動作類,這是所有你需要做的

public class UploadAction extends ActionSupport { 
     private File file; 
     private String contentType; 
     private String filename; 

     public void setUpload(File file) { 
     this.file = file; 
     } 

     public void setUploadContentType(String contentType) { 
     this.contentType = contentType; 
     } 

     public void setUploadFileName(String filename) { 
     this.filename = filename; 
     } 

     public String execute() { 
     //... 
     return SUCCESS; 
     } 
} 

上傳的文件將是治療作爲臨時文件,用一個很長的隨機文件名,你必須在你的動作類execute()方法中拷貝這個。你可以幫助FileUtils

我建議你閱讀Struts2的官方文件,上傳文件的完整配置Struts2 File-upload

+0

很好,我明白了,但你可以告訴我如何在execute()方法中將該文件[該文件是圖像文件]保存到mysql數據庫中。在Mysql數據庫中表名是PICTURE和列名是BLOB DataType的圖像... – user1184777 2012-02-04 04:36:44

+0

@ user1184777:如何在數據庫中保存圖像已在其他文章中定義 – 2012-02-04 04:43:23

相關問題