通过WebService上传文件
发布时间:2020-12-17 02:44:53 所属栏目:安全 来源:网络整理
导读:??????? 用WebService传输文件,实际上就是客户端将文件先做成比特流,然后调用webservice接口,服务端再将比特流还原成文件。下面是代码: 服务端: public ? class ?FileTransferWs? ... { ???? public ? int ?uploadFile( byte ?[]bs,?String?fileName)?
??????? 用WebService传输文件,实际上就是客户端将文件先做成比特流,然后调用webservice接口,服务端再将比特流还原成文件。下面是代码:
服务端:
public
?
class
?FileTransferWs?
{
????public?int?uploadFile(byte?[]bs,?String?fileName)?{ ????????FileOutputStream?out?=null; ????????try?{ ????????????String?newFile?"C:/tmp/+?fileName;????//上传文件存放路径 ????????????out?new?FileOutputStream(newFile); ????????????{ ????????????????out.write(bs); ????????????}catch?(IOException?e)?{ ?????????????????TODO?Auto-generated?catch?block????????????????e.printStackTrace(); ????????????} ????????}?(FileNotFoundException?e)?{ ????????????????????????e.printStackTrace(); ????????????return-1; ????????}finally??{ ????????????if?(out?!=)?{ ????????????????{ ????????????????????out.close(); ????????????????}{ ????????????????????????????????????????e.printStackTrace(); ????????????????} ????????????} ????????} ????????0; ????} } 客户端:
?FileUpload?
{
????staticvoid?main(String?[]args)?throws?Exception?{ ????????FileTransferWsProxy?p??FileTransferWsProxy();??生成webservice代理对象???????? ????????String?filePath?E:/Book/权证基础知识.pdf; ????????String?fileName?权证基础知识.pdf; ???????? ????????File?file??File(filePath); ???????? ????????FileInputStream?in??FileInputStream(file); ???????? ?????????[]bs?[in.available()]; ???????? ????????in.read(bs); ???????? ????????in.close(); ???????? ????????System.out.println(正在传输文件“?fileName?”); ????????p.uploadFile(bs,?fileName);??调用webservice进行文件上传????????System.out.println(文件传输完毕); ????} } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |