MySQL – iBatis – 文件存儲

 

把上傳的文件保存到數據庫中。數據庫的類型爲blob和longblob。上傳文件的大小大於1M是需要修改MySQL的my.ini配置文件。

修改內容如下:

 

  1. [mysqld]  
  2. max_allowed_packet=1024M  

實驗步驟:

一、創建數據庫表。

腳本:

 

  1. DROP TABLE IF EXISTS `upload`;  
  2.  
  3. CREATE TABLE `upload` (  
  4.  
  5. `dir` varchar(100) NOT NULL DEFAULT '', /* 保存上傳的時間 */  
  6.  
  7. `upload` longblob, /* 存儲文件 */  
  8.  
  9. PRIMARY KEY (`dir`)  
  10.  
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  12.  

二、建立Java項目

三、添加MySQL數據庫驅動包

四、添加iBatis包

五、添加iBatis主配置文件

代碼:

 

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE sqlMapConfig  
  3. PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"  
  4. "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> 
  5. <sqlMapConfig> 
  6. <transactionManager type="JDBC" commitRequired="false"> 
  7.   <dataSource type="SIMPLE"> 
  8.   <property name="JDBC.Driver"   value="com.mysql.jdbc.Driver"/> 
  9. <property name="JDBC.ConnectionURL" value="jdbc:mysql://10.32.52.19:3306/iBatis"/> 
  10. <property name="JDBC.Username" value="root"/> 
  11. <property name="JDBC.Password" value="root"/> 
  12. </dataSource> 
  13. </transactionManager> 
  14. <sqlMap resource="oks-sql.xml"/> 
  15. </sqlMapConfig> 

六、添加映射文件

代碼:

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE sqlMap  
  3. PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"  
  4. "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 
  5. <sqlMap namespace="sql" > 
  6. <typeAlias 
  7. alias="upload" 
  8. type="Upload" /> 
  9. <insert 
  10. id="up" 
  11. parameterClass="upload" > 
  12. INSERT INTO upload(dir,upload) values(#dir#,#uploadIn#)  
  13. </insert> 
  14. <select 
  15. id="down" 
  16. resultClass="upload" > 
  17. select dir,upload as uploadIn from upload  
  18. </select> 
  19. </sqlMap> 
  20.  

七、編寫數據表實體類。

注意:數據庫中的Blob類型和longblob類型在實體類中使用byte[]

代碼:

 

  1. import java.io.FileOutputStream;  
  2.  
  3. /**  
  4.  
  5. * TODO Put here a description of what this class does.  
  6.  
  7. *  
  8.  
  9. * @author Administrator.  
  10.  
  11. * Created Sep 12, 2012.  
  12.  
  13. */ 
  14.  
  15. public class Upload {  
  16.  
  17. private String dir;  
  18.  
  19. private byte[] uploadIn;  
  20.  
  21. private FileOutputStream uploadOut;  
  22.  
  23. /**  
  24.  
  25. * TODO Put here a description of what this constructor does.  
  26.  
  27. *  
  28.  
  29. */ 
  30.  
  31. public Upload() {  
  32.  
  33. super();  
  34.  
  35. // TODO Auto-generated constructor stub.  
  36.  
  37. }  
  38.  
  39. public String getDir() {  
  40.  
  41. return this.dir;  
  42.  
  43. }  
  44.  
  45. public void setDir(String dir) {  
  46.  
  47. this.dir = dir;  
  48.  
  49. }  
  50.  
  51. public byte[] getUploadIn() {  
  52.  
  53. return this.uploadIn;  
  54.  
  55. }  
  56.  
  57. public void setUploadIn(byte[] uploadIn) {  
  58.  
  59. this.uploadIn = uploadIn;  
  60.  
  61. }  
  62.  
  63. public FileOutputStream getUploadOut() {  
  64.  
  65. return this.uploadOut;  
  66.  
  67. }  
  68.  
  69. public void setUploadOut(FileOutputStream uploadOut) {  
  70.  
  71. this.uploadOut = uploadOut;  
  72.  
  73. }  
  74.  
  75. }  
  76.  

八、編寫連接代碼

代碼:

 

  1. private static SqlMapClient sqlMapClient;  
  2.  
  3. static {  
  4.  
  5. Reader reader = null;  
  6.  
  7. try {  
  8.  
  9. reader = Resources.getResourceAsReader("sqlMapConfig.xml");  
  10.  
  11. catch (IOException exception) {  
  12.  
  13. exception.printStackTrace();  
  14.  
  15. }  
  16.  
  17. // 得到SqlMapClient  
  18.  
  19. sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);  
  20.  
  21. }  

九、編寫上傳文件的代碼

代碼:

 

  1. public static void upload() {  
  2.  
  3. try {  
  4.  
  5. sqlMapClient.startTransaction();  
  6.  
  7. Upload upload = new Upload();  
  8.  
  9. upload.setDir(System.currentTimeMillis() + "");  
  10.  
  11. try {  
  12.  
  13. ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); //轉換byte數組  
  14.  
  15. FileInputStream in = new FileInputStream("d:/jcifs-1.3.14.jar"); //文件  
  16.  
  17. int ch;  
  18.  
  19. //轉換成byte數組  
  20.  
  21. while ((ch = in.read()) != -1) {  
  22.  
  23. bytestream.write(ch);  
  24.  
  25. }  
  26.  
  27. byte imgdata[] = bytestream.toByteArray();  
  28.  
  29. upload.setUploadIn(imgdata);  
  30.  
  31. catch (IOException exception) {  
  32.  
  33. exception.printStackTrace();  
  34.  
  35. }  
  36.  
  37. sqlMapClient.insert("up", upload);  
  38.  
  39. sqlMapClient.commitTransaction();  
  40.  
  41. catch (SQLException exception) {  
  42.  
  43. // TODO Auto-generated catch-block stub.  
  44.  
  45. exception.printStackTrace();  
  46.  
  47. }  
  48.  
  49. }  
  50.  

十、編寫下載的文件

代碼:

 

  1. public static void down() {  
  2.  
  3. try {  
  4.  
  5. byte[] Buffer = new byte[4096];  
  6.  
  7. int size = 0;  
  8.  
  9. sqlMapClient.startTransaction();  
  10.  
  11. Upload upload = (Upload) sqlMapClient.queryForObject("down");  
  12.  
  13. try {  
  14.  
  15. FileOutputStream fileOutputStream = new FileOutputStream(  
  16.  
  17. "d:/b.jar"); //保存文件  
  18.  
  19. InputStream inputStream = new ByteArrayInputStream(  
  20.  
  21. upload.getUploadIn()); //byte轉換爲流  
  22.  
  23. while ((size = inputStream.read(Buffer)) != -1) {  
  24.  
  25. System.out.println(size);  
  26.  
  27. fileOutputStream.write(Buffer, 0, size);  
  28.  
  29. }  
  30.  
  31. catch (FileNotFoundException exception) {  
  32.  
  33. // TODO Auto-generated catch-block stub.  
  34.  
  35. exception.printStackTrace();  
  36.  
  37. catch (IOException exception) {  
  38.  
  39. // TODO Auto-generated catch-block stub.  
  40.  
  41. exception.printStackTrace();  
  42.  
  43. }  
  44.  
  45. sqlMapClient.commitTransaction();  
  46.  
  47. catch (SQLException exception) {  
  48.  
  49. // TODO Auto-generated catch-block stub.  
  50.  
  51. exception.printStackTrace();  
  52.  
  53. }  
  54.  
  55. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章