EAS匯出一個單據的所有的附件

來源:互聯網
上載者:User
@Overridepublic 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);}}/** * 把從資料庫取出來的BlobObject Storage Service到應用目錄中來。通過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();}}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.