When the linux wget command sends FTP, the file containing the Chinese path prompts that the file does not exist and the file cannot be downloaded. Solution

 

The reason why the file does not exist is because the encoding is different, the server thinks that the file of the requested address does not exist.

The solution is to encode the path in a way that the service can understand (my ftp server uses gb2312), for example, use java to convert the Chinese path to English and then call wget to download:

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();
	}
}

 use

java -jar c.FTPGb2312 ftp path target file path

Instead of wget ftp path -O object file path

normal download

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326073747&siteId=291194637