⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.txt

📁 本文利用自动压缩soap附件
💻 TXT
字号:

//说明:soap方式传输数据速度慢,本文利用自动压缩soap附件,再发送数据,进而提高soap传输速率!
//本例程用于服务端。

/** 
 * web service返回client请求的下载的文件数据;以soap附件形式返回,附件自动压缩。
 * 
 * /

package axis.attachments.server;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.util.ByteArrayDataSource;

import _MyUtil.zip.GZIP;
import _MyUtil.zip.UnGZIP;







public class SendFile {
	public static String Repository = "c:\\";
	/*
	public static void main(String[] args) {
		SendFile sdf=new SendFile();
		SentFile stf= new SentFile();
		String file="c\\a.txt";
		DataHandler dh=sdf.sendFile(file);////????????????本机器上运行不成功。
		//DataHandler dh=sdf.sendFileCompress(file);
		
		//stf.doRecv(dh);
		stf.doRecvDecompress(dh);
	}
	*/
	/** 将服务器普通(非zip)文件返回到客户端中,文件-filepath(绝对路径).??
	 * */
	public DataHandler sendFileEx(String filepath) {
		System.out.println("--SendFile.sendFile() begin --");
		System.out.println("file:" + filepath);
		String Repository = "c:\\"; 
		DataHandler dh = null;
		try {
			java.io.File file = new java.io.File(filepath);

			if (file.isFile() && file.canRead()) {
				String fname = file.getAbsoluteFile().getCanonicalPath();
				System.out.println("fname:" + fname);
				DataSource ds = new FileDataSource(new File(fname));
				dh = new DataHandler(ds);
				System.out.println("dh:" + dh.getName());
			}
		} catch (Exception e) {
			System.out.println("exception--");
			System.out.println(e.getMessage());
		}
		System.out.println("retrun:"+dh.getName());
		return dh;
	}
	
	/**success!将服务器文件返回到客户端中.
	 * */
	public DataHandler sendFile(String srcFile) {
		DataHandler dh = null;
		try {
			java.io.File file = new java.io.File(srcFile);

			if (file.isFile() && file.canRead()) {

				String fname = file.getAbsoluteFile().getCanonicalPath();
				DataSource ds = new FileDataSource(new File(fname));
				dh = new DataHandler(ds);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return dh;
	}
	/** 将服务器文件压缩、返回到客户端中,文件-filepath(绝对路径)
	 * */
	public DataHandler sendFileAndCompress(String srcFile) {
		
		 byte[] bytes="1abcd阿".getBytes();
		 GZIP gzip=new GZIP();
		 bytes=gzip.compressFileToBytearray(srcFile);			// success,读取文件=>压缩=>发送
		 
		 //bytes=gzip.compressBytearrayToBytearray(bytes);		//success 压缩byte[]=>发送
			 
		 
		 DataHandler dh= new DataHandler(new ByteArrayDataSource(bytes,"application/octet-stream"));	
		//DataHandler dh = new DataHandler(new ByteArrayDataSource("wwww的".getBytes(),"application/octet-stream"));
		/*
		try {
			java.io.File file = new java.io.File(srcFile);
	
			if (file.isFile() && file.canRead()) {
				String fname = file.getAbsoluteFile().getCanonicalPath();
				
				DataSource ds=new MyFileDataSource(new File(fname)); // add lk. to create compress file.			
				
				dh = new DataHandler(new ByteArrayDataSource("wwww的".getBytes(),"application/octet-stream"));
				
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	*/
		return dh;
		
		/*
		DataHandler dh = null;
		try {
			java.io.File file = new java.io.File(srcFile);

			if (file.isFile() && file.canRead()) {

				String fname = file.getAbsoluteFile().getCanonicalPath();
				DataSource ds = new FileDataSource(new File(fname));
				dh = new DataHandler(ds);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return dh;
		*/
	}

	/** 将服务器普通(非zip)文件返回到客户端中,文件-filepath(绝对路径)
	 * */
	public DataHandler getFile(String name) {
		File dir = new File(Repository);
		if (!dir.exists())
			dir.mkdir();
		File data = new File(dir, name);
		if (data.exists())
			return new DataHandler(new FileDataSource(data));
		else
			return null;
	}
	/**将客户端文件存储到本机器中,路径-Repository、文件名- name
	 * 注意:输入参数不能包括文件路径。
	 * */

	public String putFile(DataHandler dh, String name) {
		System.out.println("--SendFile.putFile() begin --");
		if (name == null)
			name = "test.tmp";
		System.out.println("test");
		try {
			File dir = new File(Repository);
			if (!dir.exists()) {
				dir.mkdir();
				System.out.println("makedir-" + "test");
			}
			InputStream input = dh.getInputStream();
			FileOutputStream fos = new FileOutputStream(new File(dir, name));
			System.out.println("write to file:"+dir.getPath()+"\\"+name);
			byte[] buffer = new byte[1024 * 4];
			int n = 0;
			int num=0;
			while ((n = input.read(buffer)) != -1) {
				fos.write(buffer, 0, n);
				num -=n;
			}
			System.out.println("total write:"+num);
			input.close();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return name + "\n --Endpoint reveive OK!";
	}
	/**仅是处理收到的soap附件,附件被压缩。
	 * 处理:解压、并显示/保存到文件。
	 * */
	
	public void doRecvDecompress(DataHandler ret){
		UnGZIP ungzip=new UnGZIP();
		if(ret==null){
			System.out.print("server return null!!");
		}else{
			try {
				System.out.println("decompress data :"+ungzip.deCompressStreamToString(ret.getInputStream()));		// 显示解压后内容。
				//deCompressAndSaveToFile(ret.getInputStream(),"c:\\","doRecvDecompress.txt");		// 解压后内容保存到文件。
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	

	/** success;读取变量(byte[]),对其压缩,保存到变量(byte[])中返回。
	 * @throws IOException 
	 * 
	 * */
	/*
	public  String deCompressBytearrayToString(byte[] sourceBytearray) throws IOException{
		ByteArrayInputStream bytesInputStream = new ByteArrayInputStream(sourceBytearray);
		try
		{
			// 打开需压缩字节数组作为文件输入流
			GZIPInputStream gzin = new GZIPInputStream(bytesInputStream);				
			return streamToString(gzin);
	}
	catch (IOException e)
	{
		System.out.println(e);
	}
	return null;
	}
	*/

	/**sccess。将输入流解压,返回解压后的字符串内容。
	 * */
	/*
	public  String deCompress(InputStream in){	
		try
		{
			GZIPInputStream gzin = new GZIPInputStream(in);  // modify lk
			return streamToString(gzin);			
		}
		catch (IOException e)
		{
			System.out.println(e);
		}
		return null;
	}
	*/
	
	/**success.
	 * 将InputStream输入流转换成字符串String 返回。
	 * 
	 * @throws IOException
	 */
	/*
	public static String streamToString(InputStream input) throws IOException {
		String str = "";
		byte[] bytearray = new byte[1024];
		do {
			int len = input.read(bytearray, 0, 1024);
			str = str + new String(bytearray, 0, len);
			if (len < 1024)
				break;
		} while (input.available() > 0);
		return str;
	}
	*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -