请问大家如何将一大段字符生成word文档并以二进制的方式存入oracle数据库,需要时再从oracle数据库中读出并以word方式展示,非常感谢!
解决方案 »
- structs2 如何在上传图片 在同一页面显示图片
- 一个JSP传值的小问题
- 一个比较神奇的问题,关于服务器跳转
- struts2标签问题,如何展示List数据??
- 关于jsp中的request.getInputStream()上传文件的问题
- 求助 谁知道这个错误是怎么回事啊,怎么解决先谢了
- 为什么Spring API 中有 bulkUpdate 方法, 但在源码中调用不出来的?????
- 奇怪,web.xml里放多个struts-config.xml的问题:把多个struts-config用','放一起没有问题,如果用<init-param>分开就只载入一个struts-c
- jsp程序报错,找不到原因,请大家帮忙
- 在没有JDK的机子上如何运行class文件?
- s2h整合,从数据库读数据通过jsp显示,总是出错!!
- 求助大家。。。jsp引包,出现了500错误。。。
* 向数据库中插入一个BLOB对象
* @param infile 要输入的数据文件
*/
public void blobInsert(String infile) throws Exception {
FileInputStream fis = null;
try {
String sql = "insert into t_test (word) values (?)";
File f = new File(infile);
fis = new FileInputStream(f);
ps = conn.prepareStatement(sql);
ps.setBinaryStream(1,fis,fis.available()); //第二个参数为文件的内容
int resl = ps.executeUpdate();
System.out.println(resl);
} catch (Exception ex) {
System.out.println("[blobInsert error : ]" + ex.toString());
} finally {
ps.close();
fis.close();
conn.close();
}
} /**
* 从数据库中读出BLOB对象
* @param outfile 输出的数据文件
* @param id 要取的文件在数据库中的ID
* @throws java.lang.Exception
*/
public void blobRead(String outfile, int id) throws Exception {
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];
ResultSet rs = null;
File file = null;
try {
ps = conn.prepareStatement("select word from t_test where id="+id);
rs = ps.executeQuery();
if (rs.next()) {
// 把数据库的文件显示到该目录下
file = new File(outfile);
if (!file.exists()) {
file.createNewFile(); // 如果文件不存在,则创建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("word");
int size = 0;
//从数据库中一段一段的读出数据,-1表示读到了文件末
while ((size = is.read(Buffer)) != -1) {
fos.write(Buffer, 0, size);
}
}
} catch (Exception e) {
System.out.println("[OutPutFile error : ]" + e.getMessage());
} finally {
// 关闭用到的资源
fos.close();
rs.close();
ps.close();
conn.close();
}
}/**
* 生成word文档
*/
public void wordGenerate() throws IOException{
File file=new File("d:/test.docx");
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);
StringBuffer sb=null;
for(int i=0;i<10;i++){
sb=new StringBuffer();
sb.append("这是第"+i+"行:测试数据");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}
public static void main(String[] args) {
try { BlobPros blob = new BlobPros();
blob.wordGenerate();
//blob.blobInsert("D:/test.docx");
blob.blobRead("D:/test1.docx", 1);
} catch (Exception e) {
System.out.println("[Main func error: ]" + e.getMessage());
}
}