Data | database
When you need to deposit files into a server-side database, there are four options:
1.servlet/jsp+fileupload/smartupload/himself a javabean to implement the accepted file. Then call the relevant program and save the file in the database. This is also the usual choice.
2. Through the database stored procedures, directly with the operation of SQL can be implemented, the need to access the file system. See the method of depositing documents into the database in Full-text search.
3.rmi client/server approach, because the RMI to implement the interface of the parameters of the request is serializable, so you can choose byte[] or FileUpload components Fileitem objects, etc., because in the RMI is usually used to negotiate the object type, so in the file transfer, You can choose a class object that defines an inherited Seriable interface, including information about files and files.
4. Although EJB is not able to access the file system, and requires the implementation of the interface parameter requirements are serializable, It must also be a data type under the EJB specification (the basic data type) and therefore cannot be selected as a parameter by a class (not serializable) and a Class object (serialized), such as a fileupload component, java.io. However, within the EJB it is possible to use objects in the Java.io package. How to save files to a database through EJB:
1. Use byte[] as the parameter type of the remote interface.
2). Use File,fileinputstream,datoutputstream to implement file objects,
3). Then in the form of file object stream into the database.
How to implement in 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)//These two sentences can not be reversed, the above basis is to begin to read into the FileInputStream data, this sentence is the byte[] in the data read into the stream
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";
}
}
}
Invoke the EJB's client program:
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 to change the template for this generated type annotation, go to the
* Windows-Preferences-Java-code styles-code templates
*/
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 ();//Turn file into byte array
System.out.println (BT);
String Test=tr.upfile (BT, "12.xml");//Invoke EJB Program
SYSTEM.OUT.PRINTLN (test);
Tr.remove ();
}catch (Exception e) {
E.printstacktrace ();
}
}
}
The way in which the file is stored in the EJB is by turning a string or byte[into a file object and then depositing it into the database, but in the course of the operation it is noted that the EJB cannot manipulate the file system, nor does it think that the file flow cannot be manipulated in the EJB. The operational file flow probability can be reduced. When using the Java EE component, we should pay strict attention to the specification and realize the required function within the specification.