用jsp怎么编写文件下载代码
展开全部
下面是我写的一个小例子,下载远程文件urlString,到本地文件localFile.
成功返回True,不成功返回False.
把这代码插入到你JSP中用到的地方就OK了:)
public boolean downLoadFile(String urlString, String localFile) {
URL url;
byte[] buffer = new byte[512];
int size = 0;
boolean success = false;
try {
url = new URL(urlString);
BufferedInputStream stream = new BufferedInputStream(url.openStream());
FileOutputStream fos = new FileOutputStream(localFile);
while ((size = stream.read(buffer)) != -1) {
fos.write(buffer, 0, size);
}
fos.close();
stream.close();
success = true;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return success;
}
成功返回True,不成功返回False.
把这代码插入到你JSP中用到的地方就OK了:)
public boolean downLoadFile(String urlString, String localFile) {
URL url;
byte[] buffer = new byte[512];
int size = 0;
boolean success = false;
try {
url = new URL(urlString);
BufferedInputStream stream = new BufferedInputStream(url.openStream());
FileOutputStream fos = new FileOutputStream(localFile);
while ((size = stream.read(buffer)) != -1) {
fos.write(buffer, 0, size);
}
fos.close();
stream.close();
success = true;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return success;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询