现在的位置: 首页 > 综合 > 正文

EAS导出一个单据的所有的附件

2013年09月04日 ⁄ 综合 ⁄ 共 2535字 ⁄ 字号 评论关闭
	@Override
	public void exportfujian_actionPerformed(ActionEvent e) throws Exception {
		if(null==editData||null==editData.getId()){
			MsgBox.showInfo("请先保存好单据");
			SysUtil.abort();
		}
		String path=getPath();
		if(null==path){
			MsgBox.showInfo("请选择需要保存文件的路径");
			SysUtil.abort();
		}
		exportAttachment(editData.getId().toString(), path);

	}	

	/**
	 * 获取文件需要保存的文件路径
	 * @return
	 * @throws Exception
	 */
	private String getPath() throws Exception{
		String dir = ""; // 缺省路径
		Component parent = null; // Dialog上级组件
		JFileChooser chooser = new JFileChooser(dir);
		javax.swing.filechooser.FileFilter dirFilter = new javax.swing.filechooser.FileFilter() {
			public boolean accept(File f) {
				return f.isDirectory();
			}

			public String getDescription() {
				return "";
			}
		};
		chooser.setFileFilter(dirFilter);
		chooser.setFileSelectionMode(JFileChooser.SAVE_DIALOG);
		if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
			dir = chooser.getSelectedFile().getAbsolutePath();
			return dir;
		}
		return null;
	}

	
	/**
	 * 一键导出该单据的相关附件
	 * @param id 单据对应的id
	 * @param path 需要导出保存文件的路径
	 * @throws Exception
	 */
	public static void exportAttachment(String id,String path)throws Exception{
		StringBuffer sb=new StringBuffer();
		sb.append("/*dialect*/ ");
		sb.append(" select attachment.fname_l2 name, ");
		sb.append("       attachment.fsimplename type, ");
		sb.append("       attachment.ffile  contens  ");
		sb.append("from T_BAS_Attachment attachment,");
		sb.append("     T_BAS_BoAttchAsso temp ");
		sb.append("where attachment.fid=temp.fattachmentid ");
		sb.append("      and attachment.ffile is not null ");
		sb.append("      and temp.fboid='").append(id).append("'");
		IRowSet rs=DataUtils.clientExecuteSQL(sb.toString());
		while(rs.next()){
			String fileName=rs.getString("name");
			String types=rs.getString("type");
			String fullName=path+"/"+fileName+"."+types;
			File f=new File(fullName);
			int i=0;
			while(f.exists()){
				fullName=path+"/"+fileName+"("+(++i)+")."+types;
				f=new File(fullName);
			}
			Blob contends=rs.getBlob("contens");
			InputStream in=contends.getBinaryStream();
			FileOutputStream fos=new FileOutputStream(fullName);
			storeAttch(fos,in);
		}

	}
	
	/**
	 * 把从数据库取出来的Blob对象存储到应用目录中来。通过Blog对象的.getBinaryStream()方法获取二进制的输入流
	 * @param ops 字节输出流
	 * @param ips 字节输入流
	 */
	public static void storeAttch(OutputStream ops,InputStream ips){
		
		BufferedInputStream bis=new BufferedInputStream(ips);
		BufferedOutputStream bos=new BufferedOutputStream(ops);
		byte[] buf=new byte[1024*600];
		int len=0;
		try {
			while((len=bis.read(buf))!=-1){
				bos.write(buf, 0, len);
			}
		} catch (IOException e) {
			System.out.println("流文件写入、输出异常" +ips+ops);
			e.printStackTrace();
		}finally{
			if(bis!=null){
				try {
					bis.close();
				} catch (IOException e) {
					System.out.println("输入流文件关闭异常" +ips);
					e.printStackTrace();
				}
			}
			if(bos!=null){
				try {
					bos.close();
				} catch (IOException e) {
					System.out.println("输出流文件关闭异常"+ops);
					e.printStackTrace();
				}
			}
		}
		
	}

抱歉!评论已关闭.