第一种配置接口
一、先配置相关接口
配置接口名
1 2 3 4 5 6 | <service> <name>InvoiceManagementPlatformService</name> <namespace>FL.action.workflow.interfaces.weaver.com.cn</namespace> <serviceClass>weaver.interfaces.workflow.action.FL.InvoiceManagementPlatformService</serviceClass> <implementationClass>weaver.interfaces.workflow.action.FL.InvoiceManagementPlatformServiceImpl</implementationClass> </service> |
二、编写二进制流接口
创建接口实现文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | package weaver.workflow.action.FL; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.apache.commons.lang.StringUtils; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import weaver.common.DateUtil; import weaver.conn.RecordSet; import weaver.conn.RecordSetDataSource; import weaver.formmode.data.ModeDataIdUpdate; import weaver.formmode.setup.ModeRightInfo; import weaver.general.BaseBean; import weaver.general.Util; import weaver.interfaces.swfa.WorkflowUtil; import weaver.soa.workflow.request.MainTableInfo; import weaver.soa.workflow.request.Property; import weaver.soa.workflow.request.RequestInfo; import weaver.soa.workflow.request.RequestService; import weaver.workflow.webservices.WorkflowBaseInfo; import weaver.workflow.webservices.WorkflowMainTableInfo; import weaver.workflow.webservices.WorkflowRequestInfo; import weaver.workflow.webservices.WorkflowRequestTableField; import weaver.workflow.webservices.WorkflowRequestTableRecord; import weaver.workflow.webservices.WorkflowServiceImpl; @SuppressWarnings("deprecation") public class InvoiceManagementPlatformServiceImpl extends BaseBean implements InvoiceManagementPlatformService{<!-- --> /** * * 文件转二进制 * * @param fjid * @return */ public byte[] FileToBinary(String fjid) {<!-- --> RecordSet rs = new RecordSet(); String filename = "";// 文件名 String filepath = "";// 文件路径 byte[] fileByte = null; try {<!-- --> String fileSql = "select c.imagefileid,c.filerealpath,c.iszip,c.imagefilename,c.imagefile,c.filesize from DocImageFile b ,imagefile c where b.imagefileid = c.imagefileid and b.docid = " + fjid + " order by imagefileid desc"; rs.executeSql(fileSql); if (rs.next()) {<!-- --> filename = Util.null2String(rs.getString("imagefilename")); filepath = Util.null2String(rs.getString("filerealpath")); // 解压文件 UnZip(filepath, filename); // 获取解压后文件路径 String unzipfilepath = ""; writeLog("filename : " + filename + " filepath : " + filepath + " unzipfilepath : " + unzipfilepath); int dex = filepath.lastIndexOf("/"); if (dex > -1) {<!-- --> unzipfilepath = filepath.substring(0, dex) + "/" + filename; } writeLog("filename : " + filename + " filepath : " + filepath + " unzipfilepath : " + unzipfilepath); File file = new File(unzipfilepath); writeLog("zjz - filepath:" + unzipfilepath + ",filename:" + filename + ">>>" + file.exists()); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = -1; while ((len = fis.read(b)) != -1) {<!-- --> bos.write(b, 0, len); } fileByte = bos.toByteArray(); writeLog("二进制流转换成功"); } return fileByte; } catch (Exception e) {<!-- --> return fileByte; } } } |
三、测试调用代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | package weaver.interfaces.workflow.action.FL; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.net.URL; import java.util.Base64; import javax.xml.rpc.encoding.XMLType; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.codehaus.xfire.client.Client; public class DownloadFileByWebservices {<!-- --> public static void main(String[] args) {<!-- --> try {<!-- --> //Client client = new Client(new URL("http://192.168.28.11:8089//services/ManagementFeesDockingStage2Service")); //Object[] res = client.invoke("FileToBinary", new Object[]{"243913"}); Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new URL("http://192.168.28.11:8089/services/ManagementFeesDockingStage2Service")); call.setOperationName("FileToBinary"); //call.setReturnType(XMLType.XSD_BYTE); Object obj = call.invoke(new Object[]{<!-- -->"243913"}); // byte[] obj = (byte[]) call.invoke(new Object[]{"243913"}); //System.out.println(obj); byte[] bfile = Base64.getDecoder().decode(obj.toString()); // // byte[] bytes = null; // ByteArrayOutputStream byteArrayOutputStream = null; // ObjectOutputStream objectOutputStream = null; // byteArrayOutputStream = new ByteArrayOutputStream(); // objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); // objectOutputStream.writeObject(obj); // objectOutputStream.flush(); // bytes = byteArrayOutputStream.toByteArray(); byteToFile(bfile,"F:/临时/附件/cesi.pdf"); } catch (Exception e) {<!-- --> e.printStackTrace(); } } /** * @Title: byteToFile * @Description: 把二进制数据转成指定后缀名的文件,例如PDF,PNG等 * @param contents 二进制数据 * @param filePath 文件存放目录,包括文件名及其后缀,如D:\file\bike.jpg * @Author: Wiener * @Time: 2018-08-26 08:43:36 */ public static void byteToFile(byte[] contents, String filePath) {<!-- --> BufferedInputStream bis = null; FileOutputStream fos = null; BufferedOutputStream output = null; try {<!-- --> ByteArrayInputStream byteInputStream = new ByteArrayInputStream(contents); bis = new BufferedInputStream(byteInputStream); File file = new File(filePath); // 获取文件的父路径字符串 File path = file.getParentFile(); fos = new FileOutputStream(file); // 实例化OutputString 对象 output = new BufferedOutputStream(fos); byte[] buffer = new byte[1024]; int length = bis.read(buffer); while (length != -1) {<!-- --> output.write(buffer, 0, length); length = bis.read(buffer); } output.flush(); } catch (Exception e) {<!-- --> //logger.error("输出文件流时抛异常,filePath={}", filePath, e); e.printStackTrace(); } finally {<!-- --> try {<!-- --> bis.close(); fos.close(); output.close(); } catch (IOException e0) {<!-- --> //logger.error("文件处理失败,filePath={}", filePath, e0); e0.printStackTrace(); } } } } |
注意点:一开始用jdk1.6,有一些代码无法调用到方法,要切换到jdk1.8才能执行。
第二种方法
通过配置免登陆的方式跳过单点登陆进行文件下载
查看的WEB-INF的weaver_security_config.xml文件
测试文件下载地址:http://192.168.18.11:8080/weaver/weaver.file.FileDownload?requestid=159783&fileid=275791