当需要把文件存入到服务器端的数据库中,有四种方式可行: 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组件时要严格注意规范,在规范内实现需要的功能。 |
|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )
GMT+8, 2024-9-30 11:28 , Processed in 0.125716 second(s), 12 queries , Gzip On, MemCache On.
Powered by Discuz! X3.5
© 2001-2023 Discuz! Team.