java实现文件上传到ftp

  • Z8_811565
    了解作者
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-10 11:58
    上传日期
java实现文件上传到fpt,里面一些很简单的操作适合初学者。
Ftp上传.rar
  • FtpMessage.properties
    67B
  • MyFtpClient.java
    1.9KB
  • Ftp.java
    2.5KB
内容介绍
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 "上传文件成功!"; } }
评论
    相关推荐