JAVA通过WebService上传文件

来源:互联网 发布:淘宝格子铺怎么入驻 编辑:程序博客网 时间:2024/04/30 18:50
服务端public   class  FileTransferWs  { public   int  uploadFile( byte  []bs, String fileName)  {         FileOutputStream out  =   null ;          try   {             String newFile  =   " C:/tmp/ "   +  fileName;     // 上传文件存放路径              out  =   new  FileOutputStream(newFile);              try   {                 out.write(bs);             }   catch  (IOException e)  {                  //  TODO Auto-generated catch block                  e.printStackTrace();             }          }   catch  (FileNotFoundException e)  {              //  TODO Auto-generated catch block              e.printStackTrace();              return   - 1 ;         }   finally    {              if  (out  !=   null )  {                  try   {                     out.close();                 }   catch  (IOException e)  {                      //  TODO Auto-generated catch block                      e.printStackTrace();                 }              }          }           return   0 ;     }  }客户端 public   class  FileUpload  {      public   static   void  main(String []args)  throws  Exception  {         FileTransferWsProxy p  =   new  FileTransferWsProxy();   // 生成webservice代理对象                   String filePath  =   " E:/Book/权证基础知识.pdf " ;         String fileName  =   " 权证基础知识.pdf " ;                  File file  =   new  File(filePath);                  FileInputStream in  =   new  FileInputStream(file);                   byte  []bs  =   new   byte [in.available()];                  in.read(bs);                  in.close();                  System.out.println( " 正在传输文件“ "   +  fileName  +   " ” " );         p.uploadFile(bs, fileName);   // 调用webservice进行文件上传          System.out.println( " 文件传输完毕 " );     }  }


                                             
0 0
原创粉丝点击