當需要把文件存入到服務器端的數據庫中,有四種方式可行:
1.servlet/jsp+fileupload/smartupload/自己編一個實現接受文件的javabean.然后調用相關的程序,把文件存入數據庫中。這也是通常的選擇。
2.通過數據庫的存儲過程,直接用sql來操作可以實現,需要訪問文件系統。見全文搜索中向數據庫中存入文件的辦法。
3.rmi客戶/服務器的方式,由于rmi對實現的接口的參數要求是可串行化的,因此可以選用byte[]或fileupload組件中fileitem對象等,由于在rmi中通常使用雙方協商好的對象類型,因此在文件傳輸,可選用定義一繼承seriable接口的類對象,包含文件和文件的相關信息。
4.雖然ejb是不能訪問文件系統,而且要求實現的接口的參數要求是可串行化的,還必須是ejb規范下的數據類型(基本的數據類型)因此不能選用java.io包下的類(非串行化)和像fileupload組件等之外的類對象(串行化)作為參數。但是在ejb內部是可以使用java.io包中的對象。通過ejb來實現把文件存入到數據庫的方法:
1).用byte[]作為遠程接口的參數類型.
2).用file,fileinputstream,datoutputstream來實現文件對象,
3).然后以文件對象流的形式存入數據庫中。
在ejb中的實現方法:
public string upfile(byte[] filebyte,java.lang.string filename ){
try{
system.out.println("fdjkj");
file f=new file(filename);
dataoutputstream fileout=new dataoutputstream(new fileoutputstream(f));
fileinputstream fi=new fileinputstream(f);
int li=fi.read(filebyte,0,filebyte.length-1);
fileout.write(filebyte,0,filebyte.length-1);//這兩句不能顛倒,上面依據是表示開始向fileinputstream中讀入數據,這一句才是把byte[]中的數據讀入到流中
system.out.println("fdjkj");
string dname="com.microsoft.jdbc.sqlserver.sqlserverdriver";
string conurl="jdbc:microsoft:sqlserver://159.164.176.116:1038;databasename=digital lab";
// file f1=new file(""+fds.get("fileid") );
connection con=null;
statement stm=null;
resultset rs=null;
preparedstatement ps=null;
class.forname(dname).newinstance();system.out.println("fdjkj");
con=drivermanager.getconnection(conurl,"gaolong1","831001");system.out.println("fdjkj");
string sql="insert into testejbfile values('"+filename+"',?,"+(filebyte.length-1)+")";
//string sel="select * from xinxi where changhao=215;";
//string sel="select * from custom where yuming='212';";
ps=con.preparestatement(sql);system.out.println("fdsssssjkj");
ps.setbinarystream(1,fi,(int)filebyte.length-1);
// ps.setbytes(1,b);
ps.executeupdate();system.out.println("fdjkj");
ps.close();
return "ok";
}catch(exception e){
e.printstacktrace();
return "false";
}
}
}
調用ejb的客戶端程序:
package com.j2ee.first.interfaces;
import javax.naming.context;
import javax.naming.initialcontext;
import javax.rmi.portableremoteobject;
import java.util.properties;
import java.io.*;
/**
* @author gaolong1
*
* todo 要更改此生成的類型注釋的模板,請轉至
* 窗口 - 首選項 - java - 代碼樣式 - 代碼模板
*/
public class ejbclient {
public static void main(string[] args) {
try{
string url="t3://59.64.76.16:7001";
properties prop=new properties();
prop.put(context.provider_url,url);
prop.put(context.initial_context_factory,"weblogic.jndi.wlinitialcontextfactory");
context ctx=new initialcontext(prop);
object obj=ctx.lookup("ejb/com/j2ee/first/ejb/hellohome");
/* properties pr=system.getproperties();
context ctx=new initialcontext(pr);
object obj=ctx.lookup("ejb/com/fristejb/trader/ejb/traderhome");
*/
hellohome trh=(hellohome) portableremoteobject.narrow(obj,hellohome.class);
hello tr=trh.create();
system.out.println(tr.hello());
file f=new file("12.xml");
bufferedreader br=new bufferedreader(new inputstreamreader(new fileinputstream(f),"utf-8"));
string str="";
string strup="";
while((str=br.readline())!=null)
strup+=str;
system.out.println(strup);
byte[] bt=strup.getbytes();//把文件變成byte數組
system.out.println(bt);
string test=tr.upfile(bt,"12.xml");//調用ejb程序
system.out.println(test);
tr.remove();
}catch(exception e){
e.printstacktrace();
}
}
}
在ejb中實現文件存入數據庫的方法,就是通過把string或byte[]變成文件對象,然后存入到數據庫中,但在操作的過程中要注意ejb不能操作文件系統,同時也不因為這而認為在ejb中不能操作文件流。操作文件流可能性能有所下降。使用j2ee組件時要嚴格注意規范,在規范內實現需要的功能。
新聞熱點
疑難解答