java怎么从ftp下载数据_java 从ftp下载功能

1.获取连接

private staticFTPClient getConnect(String chilPath) {

FTPClient connect= newFTPClient();try{

connect.connect(AppConfig.getProperty("fp_host"),

Integer.valueOf(AppConfig.getProperty("fp_port")));

connect.login(AppConfig.getProperty("fp_username"),

AppConfig.getProperty("fp_password"));

connect.setFileType(FTPClient.BINARY_FILE_TYPE);if (!FTPReply.isPositiveCompletion(connect.getReplyCode())) {

connect.disconnect();

}if(StringUtils.isEmpty(chilPath)) {

connect.changeWorkingDirectory("/");

}else{//建立新的文件夹,FTP使用ISO8859-1编码方式

String tempPath = new String((chilPath).getBytes("UTF-8"),"ISO-8859-1");

connect.makeDirectory(tempPath);

connect.changeWorkingDirectory(tempPath);

}

}catch(Exception e) {

}returnconnect;

}

2.调用连接,下载文件(如果有父级结构)

/*** 下载文件 有子文件夹

*

*@paramfileName

* 文件名

*@paramchilPath

* 子文件夹路径

*@return

*/

public static byte[] downFile(String fileName, String chilPath) {

FTPClient connect=getConnect(chilPath);returndownFile(fileName, connect);

}

3.调用连接,下载文件(如果没有父级结构)

/*** 下载文件 无子文件夹

*

*@paramfileName

* 文件名

*@return

*/

public static byte[] downFile(String fileName) {

FTPClient connect= getConnect(null);returndownFile(fileName, connect);

}

4.真正的调用连接,获取文件在这里,以上两个方法都调用这个方法。

public static byte[] downFile(String fileName, FTPClient connect) {

InputStream in= null;try{

FTPFile[] fs=connect.listFiles(fileName);//遍历所有文件,找到指定的文件

for(FTPFile file : fs) {if(file.getName().equals(fileName)) {

connect.setBufferSize(1024);

connect.setControlEncoding("UTF-8");

in=connect.retrieveFileStream(fileName);byte[] b =input2byte(in);returnb;

}

}

}catch(Exception e) {

}finally{try{if (in != null) {

in.close();

}

connect.logout();

}catch(IOException e) {

}

}return null;

}

5.上边那个方法需要流转byte[  ],所以写了一个input2byte

public static final byte[] input2byte(InputStream inStream)throwsIOException {

ByteArrayOutputStream swapStream= newByteArrayOutputStream();byte[] buff = new byte[100];int rc = 0;while ((rc = inStream.read(buff, 0, 100)) > 0) {

swapStream.write(buff,0, rc);

}byte[] in2b =swapStream.toByteArray();returnin2b;

}

6.自动识别是否含有父级文件夹,以区别调用2还是3方法

/*** 获取指定目录文件名

*

*@parampath

*@return

*/

public staticString getFileNameByPath(String path) {//先将\\替换成/

String tempPath = path.replace("\\", "/");return path.substring(tempPath.lastIndexOf("/") + 1);

}

7.调用浏览器,下载

publicObject downloadFile(String filePath, HttpServletResponse response ) {//子目录地址

String childPath = getParentPath(filePath, 1);//文件名

String fileName =getFileNameByPath(filePath);//文件后缀名

String fileSuffixName = fileName.substring(fileName.lastIndexOf(".") + 1);try{byte[] outPutStream = null;if(childPath == null)

{//子目录不存在

outPutStream =downFile(fileName);

}else{

outPutStream=downFile(fileName, childPath);

}if(outPutStream != null)

{

response.reset();

response.setContentType("application/" + fileSuffixName + ";" + "charset = UTF-8");

response.setHeader("Content-Disposition", "attachment; filename=" +fileName);

response.getOutputStream().write(outPutStream);//以上为一种//response.reset();//response.setContentType("text/html; charset=utf-8");//response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(完整的文件名+后缀名),"utf-8");//response.getOutputStream().write(outPutStream);

return 200;

}else{

response.getWriter().write("未找到发票文件,或发票还未开出");//return 500;

}

}catch(Exception e) {

e.printStackTrace();

}return 200;

}

调用实例

1.我写了一个DownloadServlet,在doGet方法中加入以下,就可调用下载文件了。

String fp=request.getParameter("fp");if(StringUtils.isNotEmpty(fp)) {

downloadFile(fp, response);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值