package *
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import sun.net.TelnetOutputStream;
import com.fangdi.ds.util.StringUtil;
import com.fangdi.landaudit.process.affiche.FTPUtil;
import com.fangdi.landaudit.process.affiche.MyFtpClient;
import com.fangdi.workflow.dao.JdbcManager;
import com.fangdi.workflow.kernel.keygenerator.GuidKeyGenerator;
public class Ftp {
private static final Log log = LogFactory.getLog(OpinionHelper.class);
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
public static String strTime = sdf.format(new Date());
public static String path = "";
private static String server = "";
private static String username = "";
private static String password = "";
public static MyFtpClient ftpClient = new MyFtpClient();
static {
Properties properties = new Properties();
try {
properties.load(FTPUtil.class
.getResourceAsStream("FtpMessage.properties"));
} catch (IOException e) {
log.info("读取FTP配置文件出错");
e.printStackTrace();
}
server = properties.getProperty("ip");
username = properties.getProperty("username");
password = properties.getProperty("password");
path = properties.getProperty("path");
path = path +"/"+strTime;
// server:FTP服务器的IP地址;user:登录FTP服务器的用户名
// password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
ftpClient.serverAddr = server;
try {
ftpClient.openServer(server);
ftpClient.login(username, password);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 上传文件;并返回上传文件的信息
public static String upload(InputStream is, String fileName)
throws Exception {
TelnetOutputStream os = null;
try {
// path是ftp服务下主目录的子目录
if (path != null && path.length() != 0)
try {
ftpClient.issueCommand("MKD " + path);
} catch (Exception e) {
log.info(e.getMessage());
}
ftpClient.cd(path);
// 用2进制上传
ftpClient.binary();
// "upftpfile"用ftp上传后的新文件名
os = ftpClient.put(fileName);
byte[] bytes = new byte[4 * 1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
os.flush();
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return "上传文件成功!";
}
}
java实现文件上传到ftp
3星 · 超过75%的资源 需积分: 42 44 浏览量
2013-05-14
16:09:52
上传
评论
收藏 2KB RAR 举报
黑色的熊猫
- 粉丝: 5
- 资源: 6
最新资源
- SmartPlug-html大一笔记
- SmartPlug-proteusdemo
- Preliminary Findings on Handmade Rattan Baby Crib andBassinet Designs Regarding.zip
- aveebfq_v1.2.83_downyi.com.apk
- 基于有机发光二极管(OLED)的建模优化算法的matlab仿真源码+数据+文档说明+项目说明(高分课程设计)
- hash01-test.c 本人哈希表(一)的示例代码,仅供参考!
- 处理数据.py
- 软件实施计划表.xls
- 软件开发应用软件项目提交文档目录要求(全过程)(推荐文档).xls
- 软件开发项目工作量及报价模板.xls
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈