import java.io.IOException
import java.io.PrintStream
// 在当前路径(默认)创建3个非空.doc(当然也可以是.txt,.java…)文件
public class FileTest
{
//遇到异常抛给Java虚拟机处理
public static void main(String[] args)throws IOException
{
//i>-1,恒为true,创建无限文件,就成病毒了
for (int i = 0i <3i++)
{
//指定要要输入内容的文件名name
String name = "a" + i + ".doc"
//定义一个节点输出流FilOutputStream
//通俗点:定义一个水管,水管通向name文件
FileOutputStream out = new FileOutputStream(name)
//使用PrintStream包装该节点流,使用PrintStream来输出字符串
//通俗点:给水管加个水龙头(PrintStream),这个水龙头具有放水功能(ps.print())
PrintStream ps = new PrintStream(out)
ps.print("我我我窝窝窝窝窝窝窝窝哦我")
ps.append("你")
}
}
}
一切尽在代码里!
你所谓的插入word是将整个word文档插入数据库还是??还是先生成word文档 再插入数据
import java.io.BufferedInputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.sql.Blob
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import oracle.sql.BLOB
public class OracleBlob {
private DBstep.iDBManager2000 DbaObj
private File file = null
public OracleBlob() {
DbaObj=new DBstep.iDBManager2000() //创建数据库对象
}
public void insert(String infile) throws Exception {
FileInputStream fis = null
int iFileId = -1
PreparedStatement pstmt = null
ResultSet rs = null
try {
//生成随机码
java.util.Date dt=new java.util.Date()
long lg=dt.getTime()
Long ld=new Long(lg)
String mRecordID=ld.toString()
file = new File(infile)
fis = new FileInputStream(file)
//得到数据库
iFileId = DbaObj.GetMaxID("INFO_DOCUMENT_FILE","FileId")
if (DbaObj.OpenConnection()) {
DbaObj.Conn.setAutoCommit(false)
String sql="insert into INFO_DOCUMENT_FILE(FileID,RecordID,FileName,FileType,FileSize,FileDate,FileBody,FilePath,UserName,Descript) values (?,?,?,?,?,?,empty_blob(),?,?,? )"
pstmt = DbaObj.Conn.prepareStatement(sql)
pstmt.setInt(1, iFileId)
pstmt.setString(2, mRecordID)
pstmt.setString(3, mRecordID+".doc")
pstmt.setString(4, "doc")
pstmt.setLong(5,file.length())
pstmt.setDate(6, DbaObj.GetDate())
pstmt.setString(7, "")
pstmt.setString(8, "")
pstmt.setString(9, "测试通用版本==")// "通用版本"
pstmt.executeUpdate()
pstmt = DbaObj.Conn.prepareStatement("select FileBody,FileSize from INFO_DOCUMENT_FILE where RecordID='"+mRecordID+"' for update")
rs = pstmt.executeQuery()
while (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("FileBody")
OutputStream os = blob.getBinaryOutputStream()
BufferedInputStream bis = new BufferedInputStream(fis)
byte[] buf = new byte[rs.getInt("FileSize")]
int len = 0
if ((len = bis.read(buf)) != -1) {
os.write(buf, 0, len)
os.flush()
}
os.close()
bis.close()
}
DbaObj.Conn.commit()
}
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}finally{
fis.close()
DbaObj.CloseConnection()
}
}
public void readDoc(String outfile,String id)throws Exception{
FileOutputStream fos = null
InputStream is = null
String Sql = "SELECT FileBody,FileSize FROM info_document_file WHERE RecordID='" + id + "'"
try {
if (DbaObj.OpenConnection()) {
try {
ResultSet result = DbaObj.ExecuteQuery(Sql)
if (result.next()) {
try {
int fileSize=result.getInt("FileSize")
Blob blob=result.getBlob("FileBody")
file = new File(outfile)
if(!file.exists())
{
file.createNewFile()//如果文件不存在,则创建
}
fos = new FileOutputStream(file)
is = blob.getBinaryStream()// 读出数据后转换为二进制流
byte[] data = new byte[fileSize]
int size = 0
while((size = is.read(data)) != -1)
{
fos.write(data,0,size)
}
}
catch (Exception ex)
{
ex.printStackTrace()
}
}
result.close()
}
catch (SQLException e)
{
e.printStackTrace()
}
}
}
finally
{
DbaObj.CloseConnection()
}
}
public static void main(String args[])
{
OracleBlob blob=new OracleBlob()
try {
blob.readDoc("c://test.doc", "1281088075378")
//blob.insert("c://信息处WebService接口文档.doc")
} catch (Exception e) {
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)