Java实现FTP上传与下载

附件:jar包、API、Java实现下载文档 。

1、ftp4j.properties

ftp_Address=

ftp_Username=

ftp_Password=

ftp_Charset=UTF-8
ftp_Port=21


 2、ConfigUtil.java

import java.io.InputStream;
import java.util.Properties;

/**
 * 读properties文件
 *
 * @author Administrator
 *
 */
public class ConfigUtil {
 private static Properties props = new Properties();//读取配置文件的对象
 private static String defaultPropertiesName="ftp4j.properties";//默认文件名

 /**根据key关键字得到配置文件ftp4j.properties中的对应内容*/
 public static String getFtpKey(String key) {
  return getValue(defaultPropertiesName, key);
 }

 /**根据key关键字从配置文件propertiesFilename中读取对应内容*/
 public static String getValue(String propertiesFilename, String key) {
  //读取文件的输入流
  InputStream ips = ConfigUtil.class.getClassLoader().getResourceAsStream(propertiesFilename);
  try {
   props.load(ips);//加载文件
  } catch (Exception e) {//没找到此文件
   e.printStackTrace();
   return null;
  }
  return props.getProperty(key);
 }
 
}

 

3.upload

 if(pic1== null || pic1.equals(""))return "6";
  String uploadDir = ServletActionContext.getServletContext()
    .getRealPath("/" + UPLOAD_ROOT)
    + "/" + path + "/";
   FTPClient client = FTPConn.getInstance();
   try {
    // IP地址
    String address = ConfigUtil.getFtpKey("ftp_Address");
    // 端口号
    Integer port = Integer.valueOf(ConfigUtil.getFtpKey("ftp_Port"));
    // 用户名
    String username = ConfigUtil.getFtpKey("ftp_Username");
    // 密码
    String pwd = ConfigUtil.getFtpKey("ftp_Password");
    // 打开连接
    client.connect(address, port);
    // 登录服务端
    client.login(username, pwd);
    // 创建目录
    if (path != null && !path.equals("")) {
     mkdirs(client, "/"+path+"/");
    } else {
     mkdirs(client, "/"+path+"/");
    }
    // 上传图片名称
    if (pic1 != null && !pic1.equals("")) {
     client.upload(new File(uploadDir + pic1));
    }
    if (pic2 != null && !pic2.equals("")) {
     client.upload(new File(uploadDir + pic2));
    }
    client.setType(FTPClient.TYPE_AUTO);
    // 关闭连接
    client.disconnect(true);
    backString = Constants.BACKIMAGE0;//图片上传成功
   } catch (Exception e) {
    backString = Constants.BACKIMAGE1;//图片上传失败
   }
   getRequest().setAttribute("msg",backString);
 4、download

 import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import com.travel.util.ConfigUtil;


public class FileDownloadAction extends BaseAction{
 private static final long serialVersionUID = -7385634484409301915L;
 
 private String fileName;
 private String downFileName;
 private final String DOMNLOAD_ROOT = "www/www/upload/mysql";
 
 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }

 public String getDownFileName() {
  return downFileName;
 }

 public void setDownFileName(String downFileName) {
  this.downFileName = downFileName;
 }

 public InputStream getInputStream() {
  fileName = getRequest().getParameter("fileName");
  byte[] content = new byte[1024];
  try {
   downFileName = new String(fileName.getBytes(), "ISO8859-1");  //将文件名称转换编码格式,防止中文名称乱码
   content = singleDownload(DOMNLOAD_ROOT,fileName);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return new ByteArrayInputStream(content);    //返回byte数组流,共用户下载
 }
 
 public byte[] singleDownload(String path,String fileName) throws Exception{
  byte[] content = new byte[1024];
  FTPClient client = new FTPClient();
  String address = ConfigUtil.getFtpKey("ftp_Address");     // IP地址
  Integer port = Integer.valueOf(ConfigUtil.getFtpKey("ftp_Port"));  // 端口号
  String username = ConfigUtil.getFtpKey("ftp_Username");   // 用户名
  String pwd = ConfigUtil.getFtpKey("ftp_Password");   // 密码
  client.connect(address, port);    // 打开连接
  client.login(username, pwd);    // 登录服务端
  client.changeWorkingDirectory(path);  //切换到FTP物理路径
  FTPFile[] ftpFile = client.listFiles();  //获取该物理路径下的全部文件名称
  for(FTPFile ftp:ftpFile){     
   if(ftp.getName().equals(fileName)){  //找到文件名和将要下载的文件名相同的文件
    InputStream inputStream = client.retrieveFileStream(ftp.getName());   //将该文件写入流中
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);  //定义输出流
    byte[] temp = new byte[1024];
    int size = 0;
    while ((size = inputStream.read(temp)) != -1){    //将输出流写入服务器缓存
     out.write(temp, 0, size);
    }
    content = out.toByteArray();   //将缓存中文件转换为BYTE数组
   }
  }
  client.logout();
  return content;     //返回该比特数组
 }

 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  return SUCCESS;
 }
 
}

5、配置struts.xml

  <!--下载开始-->
  <action name="download" class="fileDownloadAction">
   <result name="success" type="stream">
    <param name="contentType">text/plain,image/jpeg,image/jpg</param>
    <param name="inputName">inputStream</param>
    <param name="contentDisposition">attachment;filename="${downFileName}"</param>
    <param name="bufferSize">4096</param>
   </result>
  </action>
  <!--下载结束-->  

你可能感兴趣的:(java实现)