《上传下载java.doc》由会员分享,可在线阅读,更多相关《上传下载java.doc(5页珍藏版)》请在金锄头文库上搜索。
1、文件下载:package com.ecc.liana.perbank.action;import java.io.File;import java.io.IOException;import .ftp.FTPClient;import com.ecc.emp.core.Context;import com.ecc.emp.core.EMPException;import com.ecc.liana.base.LianaAction;import com.ecc.liana.base.LianaConstants;import com.ecc.liana.base.LianaStandard;i
2、mport com.ecc.liana.file.LianaFile;import .ftp.FtpService;public class FtpDownloadAction extends LianaAction /* * 保存FTP服务器上文件位置的字段名称 */private String ftpFileField;/* * 文件传输方式:true:二进制/false:ASCII */private boolean binaryFile;/* * Method execute. * * param context * Context * return String * throws E
3、xception * see com.ecc.emp.flow.Action#execute(Context) */public String execute( Context context ) throws Exception/ 首先检查本地服务器是否已经存在要下载的文件String filePath = LianaStandard.getSelfDefineSettingsValue( LianaConstants.SETTINGS_DOWNLOAD_ROOT );String ftpFilePathName = (String) context.getDataValue( ftpFil
4、eField );String localFilePathName = filePath + ftpFilePathName;if ( !new File( localFilePathName ).isFile() )/ 如果本地服务器不存在文件,从FTP上下载FtpService ftpService = (FtpService) context.getService( LianaConstants.FTP_SERVICE_NAME );FTPClient ftpClient = null;try/ 建立本地目录结构LianaFile.makeMultiDirectory( filePath
5、, ftpFilePathName );/ 初始化连接ftpClient = ftpService.initConnection();/ 设置传输方式ftpService.setTransferMode( ftpClient, binaryFile );/ 下载文件ftpService.downloadFile( ftpClient, ftpFilePathName, localFilePathName );finallytryif ( ftpClient != null )ftpClient.logout();catch ( IOException ex )/ ex.printStackTr
6、ace();/ 保存文件位置context.setDataValue( LianaConstants.FIELD_DOWNLOAD_FILE, localFilePathName );return DEFAULT_RETURN_VALUE;/* * Method setFtpFileField. * * param ftpFileField * String */public void setFtpFileField( String ftpFileField )this.ftpFileField = ftpFileField;/* * Method setBinaryFile. * * par
7、am binaryFile * boolean */public void setBinaryFile( boolean binaryFile )this.binaryFile = binaryFile;*文件上传:package com.ecc.liana.perbank.action;import java.io.File;import java.io.IOException;import .ftp.FTPClient;import com.ecc.emp.core.Context;import com.ecc.emp.core.EMPException;import com.ecc.em
8、p.web.multipart.implement.EMPMultipartFile;import com.ecc.liana.base.LianaAction;import com.ecc.liana.base.LianaConstants;import com.ecc.liana.base.LianaStandard;import .ftp.FtpService;import com.ecc.liana.perbank.base.PBConstants;public class FtpUploadAction extends LianaAction /* * 目录分隔符 */private
9、 static final char DIR_SEPERATOR = /;/* * 上传文件的字段名称 */private String fileUploadField;/* * 保存FTP服务器上文件位置的字段名称 */private String ftpFileField;/* * 文件传输方式:true:二进制/false:ASCII */private boolean binaryFile;/* * Method execute. * * param context * Context * return String * throws EMPException * see com.ec
10、c.emp.flow.Action#execute(Context) */public String execute( Context context ) throws EMPExceptionString clusterMode = LianaStandard.getSystemParam( PB_CLUSTER_MODE );/ 获取上传文件路径和名称EMPMultipartFile uploadFile = (EMPMultipartFile) context.getDataValue( fileUploadField );String filePathName = uploadFile
11、.getTempFileName();String fileName = (new File( filePathName ).getName();String date = LianaStandard.getServerTime( yyyyMMdd );String time = LianaStandard.getServerTime( HHmmss );String customerId = (String) getSessionCtx( context ).getDataValue( PBConstants.FIELD_CUSTOMER_ID );if ( 0.equals( cluste
12、rMode ) )/ 单机模式,在本地服务器建立目录结构并移动文件String filePath = LianaStandard.getSelfDefineSettingsValue( LianaConstants.SETTINGS_DOWNLOAD_ROOT );String workingDirectory = date + DIR_SEPERATOR + time + DIR_SEPERATOR + customerId;filePath = filePath + DIR_SEPERATOR + workingDirectory;if ( !(new File( filePath ).i
13、sDirectory() )new File( filePath ).mkdirs();File downloadFile = new File( filePathName );downloadFile.renameTo( new File( filePath + DIR_SEPERATOR + fileName ) );context.setDataValue( ftpFileField, workingDirectory + DIR_SEPERATOR + fileName );else/ 集群模式,上传到FTP服务器FtpService ftpService = (FtpService) context.getService( LianaConstants.FTP_SERVICE_NAME );FTPClient ftpClient = null;try/ 初始化连接ftpClient = ftpServi