資料|資料庫
當需要把檔案存入到伺服器端的資料庫中,有四種方式可行:
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組件時要嚴格注意規範,在規範內實現需要的功能。