webservice之上传文件(byte数组上传)文件大小可随意

网上有很多webservice上传的例子,但是上传文件的大小都有限制,此方法已修改为文件大小不限制上传

先看client

设置每次发送字节数1M

byte[] buffer = new byte[1024*1024];

public static void test2(String path,String filename) {
		try {
			//一个是路径,一个是文件名
			System.out.println("axis"+path);
			RPCServiceClient serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			EndpointReference targetEPR = new EndpointReference("http://192.168.1.101:8080/axis2/services/MyAxis2Service");
			options.setTo(targetEPR);
			QName opAddEntry;
			java.io.File file = new java.io.File(path);
			java.io.FileInputStream fis = new java.io.FileInputStream(path);
			int n = 0;
			Class[] classes ;
			Object[] opAddEntryArgs ;
			//设定每上传文件的大小为1M
			byte[] buffer = new byte[1024*1024];
			while((n=fis.read(buffer))>0)
			{
				System.out.println(buffer+"buffer"+n);
				opAddEntryArgs = new Object[] { buffer,n,filename };
				classes = new Class[] { Boolean.class };
				opAddEntry = new QName("http://service.web.org","uploadImageWithByte");
				System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
			}
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
	}


再看服务端

public boolean uploadImageWithByte(byte[] imageByte, int length,String filename) {
		FileOutputStream fos = null;
		try {
			// 将上传的文件保存在D盘的文件中,注意设置为true续写文件
			fos = new FileOutputStream("d:\\"+filename,true);
			fos.write(imageByte,0, length);
			fos.close();
		} catch (Exception e) {
			return false;
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (Exception e) {

				}
			}
		}
		return true;
	}


你可能感兴趣的:(webservice)