Two methods of transporting file in webservice

First Method,

endpoint code

  public String echoBinaryAsString(byte[] bytes,String filename) {
	  String filePath = "c:/softs/img/"+filename;
      
      try {
          FileOutputStream fos = new FileOutputStream(filePath);
          BufferedOutputStream outputStream = new BufferedOutputStream(fos);
          outputStream.write(bytes);
          outputStream.close();
           
          System.out.println("Received file: " + filePath);
           
      } catch (IOException ex) {
          System.err.println(ex);
          throw new WebServiceException(ex);
      }
	    return new String(bytes);

	  }

Second Method,

Endpoint code

// Use @XmlMimeType to map to DataHandler on the client side
	   public void fileUpload(String fileName,
	                          @XmlMimeType("application/octet-stream") 
	                          DataHandler data) {
		   String filePath = "c:/softs/img/"+fileName;
		   InputStream is;
		  
		try {
			is = data.getInputStream();
			File file =new File(filePath);// File.createTempFile(filePath, "");
			   OutputStream os = new FileOutputStream(file.getAbsolutePath());
			   System.out.println(data.getName()+","+ data.getContentType()+", "+file.getAbsolutePath());
			   byte[] buffer = new byte[1024];
			   int bytesRead;
			   while ((bytesRead = is.read(buffer)) != -1)
			   {
				   //System.out.println(bytesRead);
			       os.write(buffer, 0, bytesRead);
			   }
			   os.flush();
			   os.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // DataHandler
		   
	  }



Client code

HelloWorldImplService serviceimp = new HelloWorldImplService();
		
		HelloWorldImpl service = serviceimp.getHelloWorldImplPort(new MTOMFeature());

		String teststring = service.sayHelloWorld("test from project");
		System.out.println(teststring);
		
		
        String fileName = "test.jpg";
        String filePath = "c:/softs/img/" + fileName;
        File file = new File(filePath);
         
        // uploads a file
        /*
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream inputStream = new BufferedInputStream(fis);
            byte[] imageBytes = new byte[(int) file.length()];
            inputStream.read(imageBytes);
            service.echoBinaryAsString(imageBytes,"serverpic.jpg");
           
            inputStream.close();
            System.out.println("File uploaded: " + filePath);
        } catch (IOException ex) {
            System.err.println(ex);
            ex.printStackTrace();
        } 
        */
        
        DataHandler dh = new DataHandler(new FileDataSource(filePath));
        for(int i = 0; i< 100; i++){
        service.fileUpload("dhTest"+i+".jpg", dh);
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值