ftp_文件上传Util
来源:互联网 发布:关于数据新闻的选题 编辑:程序博客网 时间:2024/05/21 11:20
import java.io.*;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
public class FtpLoadUtils {
/**
* ftp文件下载
* @param remotePath 文件路径
* @param fileName 文件索引名
* @param fileName2 文件真名
* @param response
* @return
*/
public static boolean ftpDownFile(String remotePath, String fileName,
String fileName2,HttpServletResponse response) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
// ftp.connect("127.0.0.1");
ftp.connect("196.128.1.184");
// 下面三行代码必须要,而且不能改变编码格式
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login("aa", "123");// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
response.setContentType("APPLICATION/X-msdownload;charset=UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName2, "utf-8"));
// 将文件保存到输出流outputStream中
OutputStream outputStream=response.getOutputStream();
//System.out.println(new String(fileName.getBytes("GBK"), "ISO-8859-1"));
ftp.retrieveFile(new String(fileName.getBytes("GBK"), "ISO-8859-1"), outputStream);
outputStream.flush();
outputStream.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* ftp文件上传
* @param srcFile 文件流
* @param imgmc 文件索引名
* @param path 文件存储路径
* @return
*/
public static boolean upload(File srcFile, String imgmc,String path) {
boolean result=false;
FTPClient ftpUploadClient = new FTPClient();
FileInputStream fis = null;
try {
ftpUploadClient.connect("196.128.1.x");
ftpUploadClient.login("aa", "123");// 正式name
ftpUploadClient.makeDirectory(path);
fis = new FileInputStream(srcFile);
ftpUploadClient.setBufferSize(1024);
ftpUploadClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpUploadClient.storeFile(path+ imgmc, fis);
fis.close();
ftpUploadClient.logout();
result=true;
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fis);
if(ftpUploadClient.isConnected()){
try {
ftpUploadClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
- ftp_文件上传Util
- util.js文件介绍
- Java 文件 Util
- 对文件封装util
- util文件的作用
- 【util】Java创建文件
- 文件上传---文件上传
- java.util.jar 读取文件
- MD5加密的util文件
- properties文件的读取Util
- FTP文件上传下载Util类
- 读取资源文件(util)
- 上传文件
- 文件上传
- 上传文件
- 文件上传
- 文件上传
- 上传文件
- iOS 带渐变背景动画的折线统计图
- 【LeetCode】500.Keyboard Row_EASY(三)
- 读过的最好的 C++ 开源代码
- C++在命名空间中声明类和成员函数
- 工作便签1
- ftp_文件上传Util
- f:Grid
- 微信小程序第三天(布局:栅格布局)
- CodeForces 788B Weird journey
- ubuntu_Improve Power Usage on Ubuntu 12.04 and Prolong Laptop Battery Life
- scrapy连接mysql出错
- ubuntu_UsingTheTerminal
- 博客转载
- 异常解决: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegC