JAVA通过WebService上传文件

  用WebService传输文件,实际上就是客户端将文件先做成比特流,然后调用webservice接口,服务端再将比特流还原成文件。下面是代码:
  1.  public   class  FileTransferWs  {
  2.       public   int  uploadFile( byte  []bs, String fileName)  {
  3.          FileOutputStream out  =   null ;
  4.           try   {
  5.              String newFile  =   " C:/tmp/ "   +  fileName;     // 上传文件存放路径 
  6.              out  =   new  FileOutputStream(newFile);
  7.               try   {
  8.                  out.write(bs);
  9.              }   catch  (IOException e)  {
  10.                   //  TODO Auto-generated catch block 
  11.                  e.printStackTrace();
  12.              } 
  13.          }   catch  (FileNotFoundException e)  {
  14.               //  TODO Auto-generated catch block 
  15.              e.printStackTrace();
  16.               return   - 1 ;
  17.          }   finally    {
  18.               if  (out  !=   null )  {
  19.                   try   {
  20.                      out.close();
  21.                  }   catch  (IOException e)  {
  22.                       //  TODO Auto-generated catch block 
  23.                      e.printStackTrace();
  24.                  } 
  25.              } 
  26.          } 
  27.           return   0 ;
  28.      } 
  29.  }
客户端
  1.  public   class  FileUpload  {
  2.       public   static   void  main(String []args)  throws  Exception  {
  3.          FileTransferWsProxy p  =   new  FileTransferWsProxy();   // 生成webservice代理对象 
  4.          
  5.          String filePath  =   " E:/Book/权证基础知识.pdf " ;
  6.          String fileName  =   " 权证基础知识.pdf " ;
  7.          
  8.          File file  =   new  File(filePath);
  9.          
  10.          FileInputStream in  =   new  FileInputStream(file);
  11.          
  12.           byte  []bs  =   new   byte [in.available()];
  13.          
  14.          in.read(bs);
  15.          
  16.          in.close();
  17.          
  18.          System.out.println( " 正在传输文件“ "   +  fileName  +   " ” " );
  19.          p.uploadFile(bs, fileName);   // 调用webservice进行文件上传 
  20.          System.out.println( " 文件传输完毕 " );
  21.      } 
  22.  }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值