fileservice.java

来自「精通Jboss——Ejb和Web Services开发精解的随书源代码」· Java 代码 · 共 34 行

JAVA
34
字号
package com.liuyang.axis.soapattachment;

import java.io.*;
import javax.activation.*;

public class FileService {
	public static String REPOSITORY = "./files/";
	public String putFile(DataHandler dh,String name){
		if(name==null)name="test.tmp";
		try {
			File dir = new File(REPOSITORY);
			if(!dir.exists())dir.mkdir();
			InputStream input = dh.getInputStream();	
			FileOutputStream fos = new FileOutputStream(new File(dir,name));
			byte[] buffer = new byte[1024 * 4];
			int n = 0;
			while ((n = input.read(buffer)) != -1) {
				fos.write(buffer, 0, n);
			}
			input.close();
			fos.close();					
		} catch (IOException e) {
			e.printStackTrace();
		}
		return name+" send ok";
	}
	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;
	}	
}

⌨️ 快捷键说明

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