j2EE完成文件到oracle数据库的存储

25 篇文章 0 订阅
10 篇文章 0 订阅

  今天周末休息,可以有时间写写博客。今天主要说一下利用struts2来完成文件的上传到oracle数据库。

1、前端页面的一部分,为了看着美观点,大家可以加点代码美化一下,我是加了的但是这里就只贴点重要的。

<div class="span4 offset1">
    <h2>Struts2文件上传数据库测试</h2>
    <p>
    <form id="upload_db" enctype="multipart/form-data" method="post" >
          <input type="file" id="myfile" name="myfile"/><br/>
          <input type="submit" value="上传" id="submit_db" οnclick="doUpload();">
    </form>
    </p> 
</div>

里面的doUpload()函数定义在js文件当中:

function doUpload(){
        var form  = document.getElementById("upload_db");
	form.action = "TrackUploadDBAction.action";
	form.submit();
}
2、struts2的action:

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.geoway.dao.UploadDB;
import com.opensymphony.xwork2.ActionSupport;

@Controller(value="trackUploadDBAction")
@Scope("prototype")
public class TrackUploadDBAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	@Resource(name="uploadDB")
	private UploadDB mUploadDB;
	private File myfile;
	private String myfileContentType;
	private String myfileFileName;
	
	public File getMyfile() {
		return myfile;
	}

	public void setMyfile(File myfile) {
		this.myfile = myfile;
	}

	public String getMyfileContentType() {
		return myfileContentType;
	}

	public void setMyfileContentType(String myfileContentType) {
		this.myfileContentType = myfileContentType;
	}

	public String getMyfileFileName() {
		return myfileFileName;
	}

	public void setMyfileFileName(String myfileFileName) {
		this.myfileFileName = myfileFileName;
	}

	public UploadDB getmUploadDB() {
		return mUploadDB;
	}

	public void setmUploadDB(UploadDB mUploadDB) {
		this.mUploadDB = mUploadDB;
	}

	public String UploadDB() throws SQLException, IOException, ClassNotFoundException{
		mUploadDB.insert(myfile);	
		return "success";
	}
	
}

3、文件上传的具体操作(UploadDB.java):

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import oracle.sql.CLOB;

@Repository(value="uploadDB")
@Scope("prototype")
public class UploadDB {
	
	private static Connection mConnection;
	private static Statement mStatement;
	private static PreparedStatement mPreparedStatement;
	private static ResultSet mResultSet;
	private static String url = "jdbc:oracle:thin:@localhost:1521:GEOWAY";
	private static String user = "system";
	private static String pwd = "Geoway123";
	
	public void init() throws ClassNotFoundException, SQLException{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		mConnection = DriverManager.getConnection(url, user, pwd);
		mConnection.setAutoCommit(false);
	}
	
	public boolean insert(File file) throws SQLException, IOException, ClassNotFoundException{
		init();
		String insertSql = "insert into myfile (FILECONTENT) values ( empty_clob() )";   //特别注意一下
		String selectSql = "select * from myfile where rowid = ( select max(rowid) from myfile )"; //特别注意以下
		mPreparedStatement = mConnection.prepareStatement(insertSql);
		mPreparedStatement.executeUpdate();
		mPreparedStatement.close();
		mPreparedStatement = mConnection.prepareStatement(selectSql);
		mResultSet = mPreparedStatement.executeQuery();
		BufferedReader reader = null;
		BufferedWriter write = null;
		while (mResultSet.next()){
			oracle.sql.CLOB clob = (CLOB)mResultSet.getClob("filecontent");
			reader = new BufferedReader(new FileReader(file));
			write = new BufferedWriter(clob.getCharacterOutputStream());
			int c = 0;
			while ((c = reader.read()) != -1){
				write.write(c);
			}
		}
		reader.close();
		write.close();
		mConnection.commit();
		mPreparedStatement.close();
		return true;
		
	}
}
在上面的逻辑里面要注意一下在oracle里面插入一个blob或者clob字段的方法(先插入空的,然后select出来再写入数据)。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值