emltycpqzd 发表于 2016-6-8 12:00:19

Java Ftp远程附件下载

package com.a;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class A extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -2785475707074908652L;
private FTPAgent ftpAgent = new FTPAgentImpl();
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ftpAgent.connect("66.0.42.186", 21, "posp", "posp");
String[] fileArray = ftpAgent.listFileNames("/posp/batch/file/report/20130628/20130628商户入账明细.tar");
File[] file=ftpAgent.download("/posp/batch/file/report/20130628/", "/posp/temp/", fileArray);
ftpAgent.disconnect();
File filew=file;
System.out.println("file.getName()="+filew.getName());
response.setHeader("Content-disposition", "attachment; filename=" + toUtf8String(filew.getName()));
response.setContentType("application/ms-excel; charset=\"utf-8\"");
ServletOutputStream out = response.getOutputStream();
try {
   writeFile(filew, out);
} catch (Exception e) {
   e.printStackTrace();
}
}



/**
* 文件写入指定目录
*
* @param file
* @param out
* @throws Exception
*/
public static void writeFile(File file, OutputStream out) throws Exception {
BufferedInputStream bis = null;
try {
   bis = new BufferedInputStream(new FileInputStream(file));
   int len = 0;
   byte[] buf = new byte;
   while ((len = bis.read(buf)) != -1) {
    out.write(buf, 0, len);
   }
} catch (Exception e) {
   e.printStackTrace();
} finally {
   if (bis != null)
    try {
   bis.close();
    } catch (IOException e) {

   e.printStackTrace();
    }
}

}
/**
* 导出文件防止文件名是中文时乱码的现象
*
* @param s
* @return
*/
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
   char c = s.charAt(i);
   if (c >= 0 && c <= 255) {
    sb.append(c);
   } else {
    byte[] b;
    try {
   b = Character.toString(c).getBytes("utf-8");
    } catch (Exception ex) {
   System.out.println(ex);
   b = new byte;
    }
    for (int j = 0; j < b.length; j++) {
   int k = b;
   if (k < 0)
      k += 256;
   sb.append("%" + Integer.toHexString(k).toUpperCase());
    }
   }
}
return sb.toString();
}
}

package com.a;


import java.io.File;
import java.io.IOException;
import java.text.ParseException;

import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FileTransferClient;


public class FTPAgentImpl implements FTPAgent {

private String ip;

private int port;

private String userName;

private String password;

private FileTransferClient client;

public FTPAgentImpl() {
this("", 21, "", "");
}

public FTPAgentImpl(String ip, int port, String userName, String password) {
this.ip = ip;
this.port = port;
this.userName = userName;
this.password = password;
client = new FileTransferClient();
client.getAdvancedSettings().setControlEncoding("GBK"); // FTP回显信息编码设为GBK
}

public void connect(String ip, int port, String userName, String password) {
this.ip = ip;
this.port = port;
this.userName = userName;
this.password = password;
connect();
}

public void connect() {
try {
//   System.out.println("ip:" + ip + ".port:" + port + ".name:" + userName + ".pass:" + password);
   client.setRemoteHost(ip);
   client.setRemotePort(port);
   client.setUserName(userName);
   client.setPassword(password);
   System.out.println("ip:" + ip + ".port:" + port + ".name:" + userName + ".pass:" + password);
   client.connect();
} catch (FTPException e) {
   e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}

/**
* 关闭FTP连接
*/
public void disconnect() {
try {
   client.disconnect();
} catch (FTPException e) {
   System.out.println("断开FTP连接时报错");
} catch (IOException e) {
   System.out.println("断开FTP连接时报错");
}
}

public String[] listFileNames(String dir) {
String[] fileNames = null;
try {
   fileNames = client.directoryNameList(dir, false);
} catch (FTPException e) {
   fileNames = new String[] {};
} catch (IOException e) {
   e.printStackTrace();
}
return fileNames;
}


public FTPFile[] fileList(String dir) throws ParseException {
FTPFile[] file = null;
try {
   file = client.directoryList(dir);
} catch (FTPException e) {
   file = new FTPFile[] {};
} catch (IOException e) {
   e.printStackTrace();
}
return file;
}


public File download(String remoteDir, String localDir, String fileName) {
try {
   System.out.println(localDir + fileName+"=="+remoteDir + fileName);
   client.downloadFile(localDir + fileName, remoteDir + fileName);
} catch (FTPException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

return new File(localDir + fileName);
}

public File[] download(String remoteDir, String localDir, String[] fileNames) {
if (fileNames == null || fileNames.length == 0)
   return new File[] {};
File[] files = new File;
for (int i = 0; i < fileNames.length; i++){
   File file=new File(fileNames);
   System.out.println("fileNames="+file.getName());
   
   files = download(remoteDir, localDir, file.getName());
}

return files;
}






public void upload(File localFile, String remoteDir) {
try {
   client.uploadFile(localFile.getPath(), remoteDir + localFile.getName());
} catch (FTPException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
}

@Override
public File[] download(String[] fileNames) {
// TODO Auto-generated method stub
return null;
}
}
页: [1]
查看完整版本: Java Ftp远程附件下载