linux wget命令下发FTP时,包含中文路径提示文件不存在,无法下载文件解决方法...

 

文件不存在的原因是因为编码不同,服务器认为请求地址的文件不存在。

解决方法是使用服务可以理解的方式对路径进行编码(我的ftp服务器使用的是gb2312),例如使用java先把中文路径转换为英文再调用wget进行下载:

package c;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class FTPGb2312 {

	public static void main(String[] args) {
		BufferedReader read2=null;
		BufferedReader read1=null;
		try {
			System.out.println("[info]:args[0]"+args[0]);
			args[0]=getPath(args[0]);
			System.out.println("[info]:args[0]"+args[0]);
			System.out.println("[info]:args[1]"+args[1]);
			Runtime r = Runtime.getRuntime();
			Process p=r.exec("wget --restrict-file-names=nocontrol ftp://10.231.155.209"+args[0]+" -O "+args[1]);
			read1 = new BufferedReader(new InputStreamReader(p.getInputStream(),"UTF-8"));
			String s=null;
			while((s=read1.readLine())!=null){
				System.out.println("[info]:"+s);
			}
			
			read2 = new BufferedReader(new InputStreamReader(p.getErrorStream(),"UTF-8"));
			
			while((s=read2.readLine())!=null){
				System.out.println("[info]:"+s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
			if(read1!=null){
				read1.close();
			}
			}catch(Exception e){}
			try{
			if(read2!=null){
				read2.close();
			}
			}catch(Exception e){}
		}
		
	}

	private static String getPath(String str) throws UnsupportedEncodingException {
		String[] strs=str.split("/");
		List<String> l=new ArrayList<String>();
		StringBuffer sb=new StringBuffer();
		sb.append("/");
		for (int i = 0; i < strs.length; i++) {
			if(strs[i]!=null&&!strs[i].equals("")){
				l.add(URLEncoder.encode(strs[i], "gb2312"));
			}
		}
		for (String s:l){
			sb.append("/");
			sb.append(s);
		}
		return sb.toString();
	}
}

 使用

java  -jar c.FTPGb2312 ftp路径 目标文件路径

代替 wget  ftp路径 -O 目标文件路径

即可实现正常下载

 

Linux 系统中,可以使用 `wget` 命令FTP 服务器下载文件。`wget` 支持 FTP、HTTP 和 HTTPS 协议,非常适合从网络上下载文件,尤其适合在脚本中使用。以下是使用 `wget` 从 FTP 服务器下载文件的具体方法。 ### 基本语法 ```bash wget ftp://[username:password@]hostname/path/to/file ``` ### 示例 假设需要从 FTP 服务器 `ftp.example.com` 下载位于 `/pub/files/example.txt` 的文件,用户名为 `user`,密码为 `pass`,则命令如下: ```bash wget ftp://user:pass@ftp.example.com/pub/files/example.txt ``` ### 参数说明 - `ftp://`: 指定使用 FTP 协议。 - `[username:password@]`: 如果 FTP 服务器要求身份验证,提供用户名和密码。 - `hostname`: FTP 服务器的域名或 IP 地址。 - `/path/to/file`: 文件FTP 服务器上的路径。 ### 常用选项 1. **后台下载**:使用 `-b` 选项可以让 `wget` 在后台运行,即使用户退出系统,下载任务仍将继续执行 [^2]。 ```bash wget -b ftp://user:pass@ftp.example.com/pub/files/example.txt ``` 2. **断点续传**:如果下载过程中被中断,可以使用 `-c` 选项继续下载未完成的部分 [^4]。 ```bash wget -c ftp://user:pass@ftp.example.com/pub/files/example.txt ``` 3. **指定输出文件名**:使用 `-O` 选项可以将下载文件保存为指定的文件名。 ```bash wget -O example_backup.txt ftp://user:pass@ftp.example.com/pub/files/example.txt ``` 4. **递归下载**:如果需要下载整个目录,可以使用 `-r` 选项进行递归下载。 ```bash wget -r ftp://user:pass@ftp.example.com/pub/files/ ``` 5. **使用配置文件**:`wget` 支持通过 `~/.wgetrc` 配置文件设置默认选项,例如自定义 HTTP 头信息等 [^3]。如果需要设置 FTP 代理,也可以在配置文件中进行相关配置。 ### 安全注意事项 - **密码安全**:直接在命令行中输入密码可能存在安全隐患,建议使用 `.netrc` 文件来存储 FTP 凭据信息。 ```bash machine ftp.example.com login user password pass ``` 然后使用 `--netrc` 选项启动 `wget`: ```bash wget --netrc ftp://ftp.example.com/pub/files/example.txt ``` - **权限设置**:确保 `.netrc` 文件的权限为 `600`,防止其他用户读取。 ```bash chmod 600 ~/.netrc ``` ### 示例总结 以下是一个完整的示例,展示如何使用 `wget` 从 FTP 服务器下载文件并使用断点续传功能: ```bash wget -c -O downloaded_file.txt ftp://user:pass@ftp.example.com/pub/files/target_file.txt ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值