上传文件或图片到ftp

1:读取配置文件:

      filePathConfig.properties文件最好放到src目录下,因为这是从类文件加载路径,读取配置文件,在配置文件里面写上下面数组中的配置内容即可。

 public static Map<String, String> getFtpConfig() {
        InputStream inputStream = Util.class.getClassLoader().getResourceAsStream("filePathConfig.properties");
        Properties p = new Properties();
        HashMap<String, String> env = new HashMap<String, String>();
        try {
            p.load(inputStream);
            String[] keys = new String[] { "ftphost", "ftpport", "ftpuser", "ftpassword" ,"remotepath"};
            for (String key : keys) {
                env.put(key, p.getProperty(key));
            }
        } catch (Exception e) {
        }
        
        return env;
    }

2:生成新文件名:

   参数str就是前台传过来的路径,如:C:/a/b/c.jpg
public static String upload(String str) throws Exception {
		String newFileName = "";
		if (str != null) {
			String fileName = str;
			if (StringUtils.isNotEmpty(fileName)) {
				String suffix = "";
				if (fileName.lastIndexOf(".") > 0) {
					suffix = fileName.substring(fileName.lastIndexOf("."));
				}
				String uuid = java.util.UUID.randomUUID().toString().replaceAll("-", "");
				newFileName = uuid + suffix;
			}
		}
		return newFileName;
	}

3:调用上传函数

FileInputStream in=new FileInputStream(new File(pathStr));
				flag = CommonUtil.uploadFile(Util.getFtpConfig().get("ftphost"), Integer.valueOf(Util.getFtpConfig().get("ftpport")), Util.getFtpConfig().get("ftpuser"), Util.getFtpConfig().get("ftpassword"), Util.getFtpConfig().get("remotepath"), newName, in);
public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {  
	    boolean success = false;  
	    FTPClient ftp = new FTPClient();  
	    try {  
	        int reply;  
	        ftp.connect(url, port);//连接FTP服务器  
	        //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
	        ftp.login(username, password);//登录  
	        reply = ftp.getReplyCode();  
	        if (!FTPReply.isPositiveCompletion(reply)) {  
	            ftp.disconnect();  
	            return success;  
	        }  
	        ftp.enterLocalPassiveMode();
		ftp.setFileType(FTP.BINARY_FILE_TYPE);
	        ftp.storeFile(path +filename, input);           
	          
	        input.close();  
	        ftp.logout();  
	        success = true;  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    } finally {  
	        if (ftp.isConnected()) {  
	            try {  
	                ftp.disconnect();  
	            } catch (IOException ioe) {  
	            }  
	        }  
	    }  
	    return success;  
	}



FileZilla Server的使用方法在网上很多,随便看一下就知道怎么用了,这里我想说的是我可能需要根据特定的条件将文件上传到不同的文件夹下,这个要怎么办?

假如说我们需要按条件分别上传文件到AA,BB,CC三个folder中,我们在配置文件中是配置一个用户guest,不可能为每一个文件夹都配置一个用户吧?我们只需要

找到AA,BB,CC三个folder的上一层目录,把这个目录设置为home directory(FTP),这样当FTP做登录连接的时候就会找到FTP这个目录,我们只需要把上面程序

中的remotepath参数分别传入/AA,/BB,/CC 就可以了。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值