Two methods of transporting file in webservice
发布时间:2020-12-16 22:53:59 所属栏目:安全 来源:网络整理
导读: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 B
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 } 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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |