package UIB.UIB03; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; 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.net.URL; import java.net.URLDecoder; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import UIB.COM.XmlSqlParsersFactory; import CoreFS.SA01.CoreIComponent; import CoreFS.SA06.CoreReturnObject; /** * * @desc 质保书模板管理 * @author meiguiping * @date 2010 4:21:32 PM */ public class UIB030310 extends CoreIComponent { /** * @desc 界面载入时获取配置所有文件 * @return */ public CoreReturnObject loadUIB030310() { CoreReturnObject cro = new CoreReturnObject(); cro.setResult(getFileName()); return cro; } /** * @desc 获取所有文件列表 * @return */ public String[] getFileName() { String filePath = this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "excelConfig/");//Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig"; File file = new File(filePath); String[]fileDir = file.list(); return fileDir; } /** * @desc 上传质保书、图片、配置文件等 * @param tmplNo 模板号 * @param bData 上传的文件,以byte数组形式传递 * @parm extName 文件扩展名 * @return * @throws Exception */ public CoreReturnObject upLoadFile(String tmplNo , byte[] bData , String extName , String userName) throws Exception { /* File file=new File("E:\\a.xls"); try { //创建文件输入流对象.指定要读取的文件对象 FileInputStream fin=new FileInputStream(file); //创建字节数组,准备将文件流中的数据传给字节数组 byte[] b=new byte[fin.available()]; ByteArrayOutputStream out = new ByteArrayOutputStream(); int n = 0; while ((n = fin.read(b)) != -1) { out.write(b, 0, n); } b=out.toByteArray(); bData=b; fin.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ CoreReturnObject cro = new CoreReturnObject(); this.getDao("KgDao").ExceuteNonQueryForBlobList(XmlSqlParsersFactory.getSql("UIB030310_02.UPDATE") , new Object[]{bData,extName,userName,tmplNo }); //此处没有区分上传的是质保书模板或配置文件,无论上传什么文件,图片和配置文件均重新下载,刷新一次 configFileDownload(); // Connection con = null; // PreparedStatement pstm= null; // try // { // con = this.getDao("KgDao").getConnection(); // con.setAutoCommit(false); // // pstm = con.prepareStatement(XmlSqlParsersFactory.getSql("UIB030310_02.SELECT")); // pstm.setString(1, tmplNo); // ResultSet rs = pstm.executeQuery(); // // if(rs.next()) // { // oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("TMPL_EXCEL"); // OutputStream os = blob.getBinaryOutputStream(); //// this.dbproxy.getLobHandler().getBlobAsBytes(rs , "TMPL_EXCEL"); // // // InputStream is = new ByteArrayInputStream(bData); // // byte[] bb = new byte[blob.getBufferSize()]; // int len = 0; // while((len = is.read(bb)) != -1) // { // os.write(bb , 0 , len); // } // // is.close(); // os.flush(); // os.close(); // con.commit(); // } // rs.close(); // }catch(Exception ex) // { // throw new Exception("质保书上传异常!"); // } // finally // { // try // { // if(pstm != null) // pstm.close(); // if(con !=null) // con.close(); // }catch(Exception e) // { // throw new Exception("数据库连接关闭异常!"); // } // } return cro; } /** * @desc 下载质保书、图片、配置文件等 * @param tmplNo 模板号 * @return * @throws Exception */ public CoreReturnObject downLoadFile(String tmplNo) throws Exception { Connection con = null; CoreReturnObject cro = new CoreReturnObject(); PreparedStatement pstm = null; try { con = this.getDao("KgDao").getConnection(); pstm = con.prepareStatement(XmlSqlParsersFactory.getSql("UIB030310_03.SELECT")); pstm.setString(1, tmplNo); ResultSet rs = pstm.executeQuery(); if(rs.next()) { // oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("TMPL_EXCEL"); // cro.setResult(blob.getBytes(1 , (int)blob.length())); cro.setResult(this.dbproxy.getLobHandler().getBlobAsBytes(rs , "TMPL_EXCEL")); } rs.close(); }catch(Exception ex) { throw new Exception("质保书下载异常!"); } finally { try { if(pstm !=null) pstm.close(); if(con != null) con.close(); }catch(Exception e) { throw new Exception("数据库连接关闭异常!"); } } return cro; } /** * @desc 配置文件等上传 * @param bData * @deprecated 2011-9-9 * @return */ public CoreReturnObject xmlUpLoad(byte[] bData , String fName) { CoreReturnObject cro = new CoreReturnObject(); OutputStream os = null; try { String filePath = this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "excelConfig/");//Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig"; // URL url = new URL("http://localhost:8088"); filePath =URLDecoder.decode(filePath,"utf-8"); //在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误,解决办法 os = new FileOutputStream(filePath+File.separator+fName); os.write(bData); os.close(); } catch(Exception ex) { ex.printStackTrace(); } finally { try { os.close(); }catch(Exception e) { e.printStackTrace(); } } return cro; } /** * @dresc 下载文件 * @param fileName * @deprecated 2011-9-9 * @return */ public CoreReturnObject xmlDownLoadFile(String fileName) { CoreReturnObject cro = new CoreReturnObject(); String filePath = this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "excelConfig/");//Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig"+File.separator; File file = new File(filePath+fileName); InputStream is = null; byte[] bData = new byte[(int)file.length()]; try { is = new FileInputStream(file); is.read(bData); is.close(); }catch(Exception ex) { ex.printStackTrace(); } finally { try { is.close(); }catch(Exception e) { e.printStackTrace(); } } cro.setResult(bData); return cro; } /** * @desc 配置文件、图片下载到应用服务器 * 在应用服务器启动时运行一次,以及修改后运行一次 * @throws Exception */ public void configFileDownload()throws Exception { Connection con = null; CoreReturnObject cro = new CoreReturnObject(); PreparedStatement pstm = null; try { con = this.getDao("KgDao").getConnection(); FileOutputStream fos = null; ByteArrayInputStream bis = null; pstm = con.prepareStatement(XmlSqlParsersFactory.getSql("UIB030310_04.SELECT")); ResultSet rs = pstm.executeQuery(); byte[] bytes = null; byte[] buffer = null; //String strpath = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"excelConfig//"; String queryPath = this.getClass().getClassLoader().getResource("/").getPath().replaceFirst("classes/", "excelConfig/").replaceAll("%20", " "); while(rs.next()) { bytes = this.dbproxy.getLobHandler().getBlobAsBytes(rs , "TMPL_EXCEL");//读取到字节数组 // in = new ByteArrayInputStream(bytes);// buffer = new byte[bytes.length]; fos = new FileOutputStream(queryPath+rs.getString("TMPL_CFNM")+rs.getString("EXT_NAME")); int bytesum = 0; int byteread = 0; fos.write(bytes); fos.close(); } rs.close(); }catch(Exception ex) { throw new Exception("配置文件下载异常!"); } finally { try { if(pstm !=null) pstm.close(); if(con != null) con.close(); }catch(Exception e) { throw new Exception("数据库连接关闭异常!"); } } } }