2013-07-03 30 views
0

我在通过FTP上传文件到我的VPS(centos 6 64位)时遇到了一些麻烦。我正在使用的FTP被称为Pure-FTPD,并试图使用Java上传图像。错误代码我得到的是当通过FTP上传文件到VPS时MalformedURLException

java.net.MalformedURLException:对于输入字符串:在java.net.URL中的 “VPS经过此地 @ 9X.XXX.2XX.X8” (来源不明) (Unknown Source) at java.net.URL。(Unknown Source) at FtpUrlUpload.main(FtpUrlUpload.java:25) 引起来自:java.lang.NumberFormatException:对于输入字符串:“ VPS PASS HERE @ 9X.XXX.2XX.X8“ at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) 在java.lang.Integer.parseInt(来源不明) 在java.net.URLStreamHandler.parseURL(来源不明) ... 4个

这里是我的全部代码。

import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLConnection; 

public class FtpUrlUpload { 

private static final int BUFFER_SIZE = 4096; 

public static void main(String[] args) { 
    String ftpUrl = "ftp://%s:%[email protected]%s/%s;type=i"; 
    String host = "9X.XXX.2XX.X8"; 
    String user = "VPS USERNAME HERE"; 
    String pass = "VPS PASS HERE"; 
    String filePath = "C:\\Users\\user\\desktop\\Untitled.png"; 
    String uploadPath = "public_html/Untitled.png"; 

    ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath); 
    System.out.println("Upload URL: " + ftpUrl); 

    try { 
     URL url = new URL(ftpUrl); 
     URLConnection conn = url.openConnection(); 
     OutputStream outputStream = conn.getOutputStream(); 
     FileInputStream inputStream = new FileInputStream(filePath); 

     byte[] buffer = new byte[BUFFER_SIZE]; 
     int bytesRead = -1; 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
      outputStream.write(buffer, 0, bytesRead); 
     } 

     inputStream.close(); 
     outputStream.close(); 

     System.out.println("File uploaded"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 
} 

回答

0

我假设这是由于用户名/密码中的空格未被正确编码? ()

http://docs.oracle.com/javase/6/docs/api/java/net/URI.html

+0

尝试使用URI.toURL有一个在用户名没有空格/通过它的一个短语。你也可以在这行中使用URI.toURL():ftpUrl = String.format(ftpUrl,user,pass,host,uploadPath);为用户和通行证? – user2526311