微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,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);
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐