
java如何实现txt下载 就是从远程FTP服务器上直接把TXT文本下载下来!
展开全部
strUrl为文件地址,fileName为文件在本地的保存路径,试试吧~
public static void writeFile(String strUrl, String fileName) {
URL url = null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e2) {
e2.printStackTrace();
}
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e1) {
e1.printStackTrace();
}
OutputStream os = null;
try {
os = new FileOutputStream( fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
System.out.println("下载成功:"+strUrl);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeFile(String strUrl, String fileName) {
URL url = null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e2) {
e2.printStackTrace();
}
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e1) {
e1.printStackTrace();
}
OutputStream os = null;
try {
os = new FileOutputStream( fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
System.out.println("下载成功:"+strUrl);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
去看一下ftp协议的客户端部分,然后你简单实现一个ftp客户端,就可以下载文件了。大概原理就是这样,剩下的就是网上搜搜相关资料了。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询