webservices传输文件

 

@XmlRootElement(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer implements Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private String name;
    @XmlMimeType("application/octet-stream")
    private DataHandler myData; 
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public DataHandler getMyData() {
		return myData;
	}
	public void setMyPData(DataHandler myData) {
		this.myData = myData;
	}
    
}

 

@javax.xml.ws.soap.MTOM
@WebService(name = "MTOMServerService", portName = "MTOMServer")
public class MTOMServer {
	static Logger logger = LogManager.getLogger(MTOMServer.class.getName());
	private static final int BUFFER_SIZE = 1024*1024*100;
	
	/**
	 * 删除
	 * @param path 文件夹路径
	 * @throws FileNotFoundException
	 */
	@WebResult
	@XmlMimeType("*/*")
	public void delete(@WebParam(name = "path") String path) throws FileNotFoundException {
		
		if (path == null || path.isEmpty()){
			logger.error(path+"文件路径为空!");
			throw new FileNotFoundException("文件路径为空!");
		}
		File downloadFile = new File(path);
		
		if (!downloadFile.exists()){
			logger.error(path+"不存在!");			
			throw new FileNotFoundException(path + "不存在!");
		}
		logger.debug("删除:"+path);
		
		(new PowerDelete()).delete(path);
	}

	/**
	 * 下载文件
	 * @param path 文件夹路径
	 * @throws FileNotFoundException
	 * @return DataHandler 数据句柄
	 */
	@WebResult
	@XmlMimeType("*/*")
	public Customer download(@WebParam(name = "path") String path) throws FileNotFoundException {
		
		if (path == null || path.isEmpty()){
			logger.error(path+"文件路径为空!");			
			throw new FileNotFoundException("文件路径为空!");
		}

		final File downloadFile = new File(path);
		
		if (!downloadFile.exists()){
			logger.error(path+"不存在!");			
			throw new FileNotFoundException(path + "不存在!");
		}

		if (!downloadFile.isFile()){
			logger.error(path+"不是一个标准文件!");			
			throw new FileNotFoundException(path + "不是一个标准文件!");
		}
		
		logger.debug("下载文件:"+path);
		
		DataHandler dataHandler = new DataHandler(new FileDataSource(downloadFile));
		
		Customer customer = new Customer();
		
		customer.setName(downloadFile.getName());
		customer.setMyPData(dataHandler);
		
		return customer;
	}

	/**
	 * 
	 * 下载全部文件
	 * @param path 文件夹路径
	 * @throws FileNotFoundException
	 * @return DataHandler[] 数据句柄
	 */
	@WebResult
	@XmlMimeType("*/*")
	public Customer[] downloadMulti(@WebParam(name = "path") String path) throws FileNotFoundException {
		
		if (path == null || path.length() <= 0) {
			logger.error(path+"文件路径为空!");	
			throw new FileNotFoundException("文件夹路径为空!");
		}
		
		File file = new File(path);
		if (!file.exists()){
			logger.error(path+"不存在!");
			throw new FileNotFoundException(path + "不存在!");
		}
		if (file.isFile()){
			logger.error(path+"是一个标准文件,请更换下载方法!");	
			throw new FileNotFoundException(path + "是一个标准文件,请更换下载方法!");
		}
		
		logger.debug("下载文件夹:"+path);
		
		final File[] files = file.listFiles();
		Customer[] handlers = new Customer[files.length];

		for (int i = 0, j = files.length; i < j; i++) {
			final String fileName = files[i].getName();
			Customer customer = new Customer();
			DataHandler dataHandler = new DataHandler(new FileDataSource(files[i]));
			customer.setName(fileName);
			customer.setMyPData(dataHandler);
			handlers[i] = customer;
		}
		
		return handlers;
	}
	
	/**
	 * 上载
	 * 
	 * @param fileName 待上载的文件名
	 * @param dataHandler 数据句柄
	 * @throws IOException IO异常
	 */
	public String upload(@WebParam(name = "fileName") String fileName, @XmlMimeType("*/*") @WebParam(name = "dataHandler") DataHandler dataHandler){
		logger.debug("上传文件:"+fileName);
		
		File depository = ConfigManager.getFileDepository();
		String urlString = depository.getAbsolutePath() + File.separatorChar + fileName;
		InputStream in = null;
		OutputStream out = null;
		try {
			if (fileName == null || fileName.isEmpty()){
				logger.error("文件名为空!");
				throw new FileNotFoundException("文件名为空!");
			}
			
			in = dataHandler.getInputStream();
			out = new FileOutputStream(urlString);
			
			byte[] buf = new byte[BUFFER_SIZE];
			int read;
			while ((read = in.read(buf)) != -1) {
				out.write(buf, 0, read);
				out.flush();
			}
		} catch (IOException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return urlString;
	}
}

 

		// 下载全部文件
		try {
			List<Customer> dataHandlers = mtomPort.downloadMulti("W:\\TEST\\20140117-144703-4DCD");

			byte[] buf = new byte[CHUNK_SIZE];
			for (int i = 0, j = dataHandlers.size(); i < j; i++) {
				Customer handler = dataHandlers.get(i);
				String fileName = handler.getName();
				fileName = fileName == null || fileName.isEmpty() ? Integer.toString(i) : fileName;
				InputStream in = handler.getMyData().getInputStream();
				OutputStream out = new FileOutputStream("G:\\"+fileName);

				int read;
				while ((read = in.read(buf)) != -1) {
					out.write(buf, 0, read);
					out.flush();
				}
				in.close();
				out.close();
			}
		} catch (FileNotFoundException_Exception e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值