把上傳的文件保存到數據庫中。數據庫的類型爲blob和longblob。上傳文件的大小大於1M是需要修改MySQL的my.ini配置文件。
修改內容如下:
- [mysqld]
- max_allowed_packet=1024M
實驗步驟:
一、創建數據庫表。
腳本:
- DROP TABLE IF EXISTS `upload`;
- CREATE TABLE `upload` (
- `dir` varchar(100) NOT NULL DEFAULT '', /* 保存上傳的時間 */
- `upload` longblob, /* 存儲文件 */
- PRIMARY KEY (`dir`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
二、建立Java項目
三、添加MySQL數據庫驅動包
四、添加iBatis包
五、添加iBatis主配置文件
代碼:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE sqlMapConfig
- PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
- <sqlMapConfig>
- <transactionManager type="JDBC" commitRequired="false">
- <dataSource type="SIMPLE">
- <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
- <property name="JDBC.ConnectionURL" value="jdbc:mysql://10.32.52.19:3306/iBatis"/>
- <property name="JDBC.Username" value="root"/>
- <property name="JDBC.Password" value="root"/>
- </dataSource>
- </transactionManager>
- <sqlMap resource="oks-sql.xml"/>
- </sqlMapConfig>
六、添加映射文件
代碼:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMap
- PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-2.dtd">
- <sqlMap namespace="sql" >
- <typeAlias
- alias="upload"
- type="Upload" />
- <insert
- id="up"
- parameterClass="upload" >
- INSERT INTO upload(dir,upload) values(#dir#,#uploadIn#)
- </insert>
- <select
- id="down"
- resultClass="upload" >
- select dir,upload as uploadIn from upload
- </select>
- </sqlMap>
七、編寫數據表實體類。
注意:數據庫中的Blob類型和longblob類型在實體類中使用byte[]
代碼:
- import java.io.FileOutputStream;
- /**
- * TODO Put here a description of what this class does.
- *
- * @author Administrator.
- * Created Sep 12, 2012.
- */
- public class Upload {
- private String dir;
- private byte[] uploadIn;
- private FileOutputStream uploadOut;
- /**
- * TODO Put here a description of what this constructor does.
- *
- */
- public Upload() {
- super();
- // TODO Auto-generated constructor stub.
- }
- public String getDir() {
- return this.dir;
- }
- public void setDir(String dir) {
- this.dir = dir;
- }
- public byte[] getUploadIn() {
- return this.uploadIn;
- }
- public void setUploadIn(byte[] uploadIn) {
- this.uploadIn = uploadIn;
- }
- public FileOutputStream getUploadOut() {
- return this.uploadOut;
- }
- public void setUploadOut(FileOutputStream uploadOut) {
- this.uploadOut = uploadOut;
- }
- }
八、編寫連接代碼
代碼:
- private static SqlMapClient sqlMapClient;
- static {
- Reader reader = null;
- try {
- reader = Resources.getResourceAsReader("sqlMapConfig.xml");
- } catch (IOException exception) {
- exception.printStackTrace();
- }
- // 得到SqlMapClient
- sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
- }
九、編寫上傳文件的代碼
代碼:
- public static void upload() {
- try {
- sqlMapClient.startTransaction();
- Upload upload = new Upload();
- upload.setDir(System.currentTimeMillis() + "");
- try {
- ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); //轉換byte數組
- FileInputStream in = new FileInputStream("d:/jcifs-1.3.14.jar"); //文件
- int ch;
- //轉換成byte數組
- while ((ch = in.read()) != -1) {
- bytestream.write(ch);
- }
- byte imgdata[] = bytestream.toByteArray();
- upload.setUploadIn(imgdata);
- } catch (IOException exception) {
- exception.printStackTrace();
- }
- sqlMapClient.insert("up", upload);
- sqlMapClient.commitTransaction();
- } catch (SQLException exception) {
- // TODO Auto-generated catch-block stub.
- exception.printStackTrace();
- }
- }
十、編寫下載的文件
代碼:
- public static void down() {
- try {
- byte[] Buffer = new byte[4096];
- int size = 0;
- sqlMapClient.startTransaction();
- Upload upload = (Upload) sqlMapClient.queryForObject("down");
- try {
- FileOutputStream fileOutputStream = new FileOutputStream(
- "d:/b.jar"); //保存文件
- InputStream inputStream = new ByteArrayInputStream(
- upload.getUploadIn()); //byte轉換爲流
- while ((size = inputStream.read(Buffer)) != -1) {
- System.out.println(size);
- fileOutputStream.write(Buffer, 0, size);
- }
- } catch (FileNotFoundException exception) {
- // TODO Auto-generated catch-block stub.
- exception.printStackTrace();
- } catch (IOException exception) {
- // TODO Auto-generated catch-block stub.
- exception.printStackTrace();
- }
- sqlMapClient.commitTransaction();
- } catch (SQLException exception) {
- // TODO Auto-generated catch-block stub.
- exception.printStackTrace();
- }
- }