广告位 后台主题配置管理 |
广告位 后台主题配置管理 |
本篇文章给大家谈谈ftp服务器下载jsp,以及ftp服务器下载的zip文件无法解压对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
form表单提交文件,建议用smartupload上传,暂存在web服务器目录下,然后稍微一下下面的代码,ftp上传后,删除暂存文件,ok
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
import org.apache.commons.net.;
import org.apache.commons.net.;
import org.apache.commons.net.;
import org.apache.log4j.Logger;
/**
* Ftp 服务类,对Apache的commons.net.ftp进行了包装br
* 依赖库文件:commons-net-1.4.1.jar
*
* @version 1.0 2008-02-18
* @author huchao@jbsoft
*/
public class FtpService {
public FtpService(String serverAddr, String lsenport, String userName,
String pswd) {
this.ftpServerAddress = serverAddr;
this.port = Integer.parseInt(lsenport);
this.user = userName;
this.password = pswd;
}
/**
* FTP 服务器地址
*/
private String ftpServerAddress = null;
/**
* FTP 服务端口
*/
private int port = 21;
/**
* FTP 用户名
*/
private String user = null;
/**
* FTP 密码
*/
private String password = null;
/**
* FTP 数据传输超时时间
*/
private int timeout = 0;
/**
* 异常:登录失败
*/
private final I2HFException EXCEPTION_LOGIN = new I2HFException("COR101",
"FTP服务器登录失败");
/**
* 异常:文件传输失败
*/
private final I2HFException EXCEPTION_FILE_TRANSFER = new I2HFException(
"COR010", "FTP文件传输失败");
/**
* 异常:IO异常
*/
private final I2HFException EXCEPTION_GENERAL = new I2HFException("COR010",
"FTP IO 异常");
private static final Logger logger = Logger.getLogger(FtpService.class);
/**
* 初始化FTP连接,并进行用户登录
*
* @return FTPClient
* @throws I2HFException
*/
public FTPClient initConnection() throws I2HFException {
FTPClient ftp = new FTPClient();
try {
// 连接到FTP
(ftpServerAddress, port);
int reply = ;
if (!FTPReply.isPositiveCompletion(reply)) {
;
throw new I2HFException("COR010", "FTP服务器连接失败");
}
// 登录
if (!(user, password)) {
throw EXCEPTION_LOGIN;
}
// 传输模式使用passive
;
// 设置数据传输超时时间
;
logger.info("FTP服务器[" + ftpServerAddress + " : " + port + "]登录成功");
} catch (I2HFException te) {
logger.info(te.errorMessage, te);
throw te;
} catch (IOException ioe) {
logger.info(ioe.getMessage(), ioe);
throw EXCEPTION_LOGIN;
}
return ftp;
}
/**
* 设置传输方式
*
* @param ftp
* @param binaryFile
* true:二进制/false:ASCII
* @throws I2HFException
*/
public void setTransferMode(FTPClient ftp, boolean binaryFile)
throws I2HFException {
try {
if (binaryFile) {
;
logger.info("FTP文件传输方式为:二进制");
} else {
;
logger.info("FTP文件传输方式为:ASCII");
}
} catch (IOException ex) {
logger.info(ex.getMessage(), ex);
throw EXCEPTION_GENERAL;
}
}
/**
* 在当前工作目录下建立多级目录结构
*
* @param ftp
* @param dir
* @throws I2HFException
*/
public void makeMultiDirectory(FTPClient ftp, String dir)
throws I2HFException {
try {
StringBuffer fullDirectory = new StringBuffer();
StringTokenizer toke = new StringTokenizer(dir, "/");
while (toke.hasMoreElements()) {
String currentDirectory = (String) toke.nextElement();
fullDirectory.append(currentDirectory);
(fullDirectory.toString());
if (toke.hasMoreElements()) {
fullDirectory.append('/');
}
}
} catch (IOException ex) {
logger.info(ex.getMessage(), ex);
throw EXCEPTION_GENERAL;
}
}
/**
* 更改服务器当前路径
*
* @param ftp
* @param dir
* @throws I2HFException
*/
public void changeWorkingDirectory(FTPClient ftp, String dir)
throws I2HFException {
try {
if (!) {
throw new I2HFException("COR010", "目录[ " + dir + "]进入失败");
}
} catch (I2HFException tfe) {
logger.info(tfe.errorMessage, tfe);
throw tfe;
} catch (IOException ioe) {
logger.info(ioe.getMessage(), ioe);
throw EXCEPTION_GENERAL;
}
}
/**
* 上传文件到FTP服务器
*
* @param ftp
* @param localFilePathName
* @param remoteFilePathName
* @throws I2HFException
*/
public void uploadFile(FTPClient ftp, String localFilePathName,
String remoteFilePathName) throws I2HFException {
InputStream input = null;
try {
input = new FileInputStream(localFilePathName);
boolean result = (remoteFilePathName, input);
if (!result) {
// 文件上传失败
throw EXCEPTION_FILE_TRANSFER;
}
logger.info("文件成功上传到FTP服务器");
} catch (I2HFException tfe) {
logger.info(tfe.getMessage(), tfe);
throw tfe;
} catch (IOException ioe) {
logger.info(ioe.getMessage(), ioe);
throw EXCEPTION_FILE_TRANSFER;
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ex) {
logger.info("FTP对象关闭异常", ex);
}
}
}
/**
* 下载文件到本地
*
* @param ftp
* @param remoteFilePathName
* @param localFilePathName
* @throws I2HFException
*/
public void downloadFile(FTPClient ftp, String remoteFilePathName,
String localFilePathName) throws I2HFException {
boolean downloadResult = false;
OutputStream output = null;
try {
output = new FileOutputStream(localFilePathName);
downloadResult = (remoteFilePathName, output);
if (!downloadResult) {
// 如果是文件不存在将异常抛出
throw new I2HFException("COR011", "文件不存在");
}
logger.info("文件成功从FTP服务器下载");
} catch (I2HFException tfe) {
logger.error(tfe.getMessage(), tfe);
throw tfe;
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
throw EXCEPTION_FILE_TRANSFER;
} finally {
try {
if (output != null) {
output.close();
}
if (!downloadResult) {
new File(localFilePathName).delete();
}
} catch (IOException ex) {
logger.error("FTP对象关闭异常", ex);
}
}
}
/**
* Method setFtpServerAddress.
*
* @param ftpServerAddress
* String
*/
public void setFtpServerAddress(String ftpServerAddress) {
this.ftpServerAddress = ftpServerAddress;
}
/**
* Method setUser.
*
* @param user
* String
*/
public void setUser(String user) {
this.user = user;
}
/**
* Method setPassword.
*
* @param password
* String
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Method setTimeout.
*
* @param timeout
* String
*/
public void setTimeout(String timeout) {
try {
this.timeout = Integer.parseInt(timeout);
} catch (NumberFormatException ex) {
// 默认超时时间500毫秒
this.timeout = 500;
}
}
/**
* Method setPort.
*
* @param port
* String
*/
public void setPort(String port) {
try {
this.port = Integer.parseInt(port);
} catch (NumberFormatException ex) {
// 默认端口21
this.port = 21;
}
}
}
=====================================
jsp上传部分
===================================
form method="post" name="uploadform" id="uploadform" action="upload" ENCTYPE="multipart/form-data"
input type="hidden" name="service" value="com.jbsoft.i2hf.corpor.services.CstBatchBizServices.exclUpload"/
table cellspacing="0" cellpadding="0"
tr
td width="20%" align="right"上传本地文件:/td
td width="80%"input class="qnphpbfc8-1dcd-77db-67ff " style="width:300px" type="file" width="300px" name="exclname" id="exclname"//td
/tr
/table
/form
============================================
上传的servlet用的是smartupload
,部分代码可以参考一下:
==========================================
SmartUpload su = new SmartUpload();
su.setCharset("UTF-8");
su.initialize(getServletConfig(), request, response);
su.setMaxFileSize(10240000);
su.setTotalMaxFileSize(102400000);
su.setAllowedFilesList("xls");
su.upload();
===========================================
代码里面有一些客户的信息,不能全部给你哈
前段时间做了一个文件上传ftp功能,你参照一下
package com.;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.;
import org.apache.commons.net.;
import org.apache.commons.net.;
public class FTPUpload {
private FTPClient ftpClient = null;
private OutputStream outSteam = null;
/**
* ftp服务器地址
*/
private String hostname = "192.168.1.2";
/**
* ftp服务器端口
*/
int port = 21;
/**
* 登录名
*/
private String username = "admin";
/**
* 登录密码
*/
private String password = "admin";
/**
* 需要访问的远程目录
*/
private String remoteDir = "/home/demo";
public FTPUpload() { }
public FTPUpload(String hostname, int port, String username, String password, String remoteDir){
this.hostname = hostname;
this.port = port;
this.username = username;
this.password = password;
this.remoteDir = remoteDir;
}
/**
* 连接FTP服务器 并登录
* @param hostName FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
*/
private FTPClient connectFTPServer() {
try {
//1.创建FTPClient对象
ftpClient = new FTPClient();
//2.连接FTP服务器
// 如果采用默认端口,可以使用的方式直接连接FTP服务器
ftpClient.connect(hostname, port); //链接到ftp服务器
// System.out.println("连接到ftp服务器地址 -- ftp://" + hostName + ":" + port + " 成功..开始登录");
//3.判断连接ftp服务器是否成功
int reply = ftpClient.getReplyCode();
// System.out.println("以2开头的返回值就会为真:" + reply);
//以2开头的返回值就会为真
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return null;
}
//4.登录FTP服务器.用户名 密码
ftpClient.login(username, password);
System.out.println("登录成功." );
return ftpClient;
} catch (Exception e) {
e.printStackTrace();
ftpClient = null;
return ftpClient;
}
}
/**
* 向FTP服务器上传文件
* @param filePathName 上传文件的全路径名称
* @return 成功返回true,否则返回false
*/
public boolean uploadFile(String filePathName) {
// 初始表示上传失败
boolean success = false;
try {
// 创建FTPClient对象
ftpClient = connectFTPServer();
//创建文件夹
boolean flag = ftpClient.makeDirectory(remoteDir);
if(flag) {
System.out.println("创建文件夹:" + remoteDir );
}
// 转到指定上传目录
boolean flag0 = ftpClient.changeWorkingDirectory(remoteDir);
// 将上传文件存储到指定目录
if(filePathName == null || filePathName.length() == 0){
return success;
}
String filename = filePathName.substring(filePathName.replace("\\", "/").lastIndexOf("/") + 1, filePathName.length());
InputStream input = new FileInputStream(new File(filePathName));
// ftpClient.setBufferSize(1024);
// ftpClient.setControlEncoding("GBK");
// ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件类型
boolean flag1 = ftpClient.storeFile(filename, input);
System.out.println("转到指定上传目录:" + flag0 + " 将上传文件存储到指定目录:" + flag1);
input.close(); // 关闭输入流
ftpClient.logout(); // 退出ftp
success = true; // 表示上传成功
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* 从FTP服务器指定目录下载文件 到本地目录中 OK
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @param showlist 下载时是否显示列表 ( true 显示 )
* @return
*/
public boolean downFile(String fileName, String localPath, boolean showlist) {
// 初始表示下载失败
boolean success = false;
if(fileName == null || fileName.length() == 0 || localPath == null || localPath.length() == 0){
return success;
}
try {
File file = new File(localPath);
if(!file.isDirectory()){
file.mkdir();
}
// 创建FTPClient对象
ftpClient = connectFTPServer();
// 转到指定下载目录
boolean flag = ftpClient.changeWorkingDirectory(remoteDir);
if(!flag) {
System.out.println("目录:" + remoteDir +"不存在!");
return success;
}
// 列出该目录下所有文件
FTPFile[] remoteFiles = ftpClient.listFiles();
// 遍历所有文件,找到指定的文件
if(showlist){
System.out.println("目录" + remoteDir + "下的文件:");
}
for (FTPFile ftpFile : remoteFiles) {
String name = ftpFile.getName();
if(showlist){
long length = ftpFile.getSize();
String readableLength = FileUtils.byteCountToDisplaySize(length);
System.out.println(name + ":\t\t" + readableLength);
}
if (name.equals(fileName)) {
// 根据绝对路径初始化文件
File localFile = new File(localPath + "/" + name);
// 输出流
OutputStream is = new FileOutputStream(localFile);
// 下载文件
ftpClient.retrieveFile(name, is);
is.close();
}
}
// 退出ftp
ftpClient.logout();
// 下载成功
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* 显示FTP服务器指定文件夹中的文件及大小 OK
* @return
*/
private boolean showFileList() {
// 初始表示失败
boolean success = false;
try {
ftpClient = connectFTPServer();
FTPFile[] remoteFiles = null;
// 转到指定下载目录
boolean flag = ftpClient.changeWorkingDirectory(remoteDir);
if(!flag) {
System.out.println("目录:" + remoteDir +"不存在!");
return success;
} else{
remoteFiles = ftpClient.listFiles(remoteDir);
System.out.println("目录" + remoteDir + "下的文件:");
}
if(remoteFiles != null) {
for(int i=0;i remoteFiles.length; i++){
String name = remoteFiles[i].getName();
long length = remoteFiles[i].getSize();
String readableLength = FileUtils.byteCountToDisplaySize(length);
System.out.println(name + ":\t\t" + readableLength);
}
}
// 表示成功
success = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
//使用IO包关闭流
IOUtils.closeQuietly(outSteam);
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return success;
}
public static void main(String[] args) {
FTPUpload ftp = new FTPUpload();
("c:////test////bgssp.jar");
}
}
按住shift然后右键单击文件,就会出现打开方式...选项,用windows的记事本打开(就是打开txt文件的那种),看看有没有内容
关于ftp服务器下载jsp和ftp服务器下载的zip文件无法解压的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
广告位 后台主题配置管理 |
广告位 后台主题配置管理 |