我使用下面的代码写入FTP服务器中存在的文本文件。但是得到了java.net.MalformedURLException
URL url = new URL("ftp://p@g.com:g@1234@ftp.xyz.com/testjar/2014-03-06-p.txt;type=i");
URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream(); // To upload
OutputStream buffer = new BufferedOutputStream(os);
ObjectOutput output = new ObjectOutputStream(buffer);
output.writeObject("hiiiii");
buffer.close();
os.close();
output.close();
上面的用户名和密码不是真实的,但它的演示看起来像真的。 任何人都知道如何解决这个问题或其他方法写入.txt文件,让我告知。
Edit1:我的用户名和密码也包含@ char,并且传递号码在那里。 完全错误:
java.net.MalformedURLException: For input string: "g@1234@ftp.xyz.com"
at java.net.URL.<init>(URL.java:619)
at java.net.URL.<init>(URL.java:482)
at java.net.URL.<init>(URL.java:431)
at CreateFolder.uploadfile(CreateFolder.java:39)
at CreateFolder.main(CreateFolder.java:74)
Caused by: java.lang.NumberFormatException: For input string: "g@1234@ftp.xyz.com"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at java.net.URLStreamHandler.parseURL(URLStreamHandler.java:217)
at java.net.URL.<init>(URL.java:614)
... 4 more
答案 0 :(得分:0)
您没有向我们提供您收到的错误,但我在此处看到了几个问题:
ObjectOutputStream
类旨在编写对象数据,以便ObjectInputStream
(see here)重建它。它不是用于编写文本文件。如果你需要的只是编写String来流更好地使用PrintStream 所以修改后的例子将是:
try {
URL url = new URL("ftp://user:pass@ftp.xyz.com/testjar/2014-03-06-p.txt;type=i");
URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream(); // To upload
OutputStream buffer = new BufferedOutputStream(os);
PrintStream output = new PrintStream(buffer);
output.print("hiiiii");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
output.close();
buffer.close();
os.close();
}
答案 1 :(得分:0)
解决: 替换&#39; @&#39;带有&#39;%40&#39;的字符在解析时,它会自动转换为&#39; @&#39;。
URL url = new URL("ftp://p%40g.com:g%401234@ftp.xyz.com/testjar/2014-03-06-p.txt;type=i");
URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream(); // To upload
OutputStream buffer = new BufferedOutputStream(os);
PrintStream output = new PrintStream(buffer);
output.print("wowhii");
buffer.close();
os.close();
output.close();