java实现从远程tomcat服务器下载文件到本地

我就是个打字的 2012-08-09 10:05:04
tomcat 发布项目 test的 webroot/upload目录下有文件 test.xml
现在要从java中实现 从服务器目录下把文件下载到本机上, 本人刚入行, 求帮助代码怎么实现
...全文
630 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
如果我的服务器在 10.11.11这个机器上,而我本机是 10.11.12 上述方法还能用吗? 远程下载
Sammie 2012-08-09
  • 打赏
  • 举报
回复


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/basic.css" rel="stylesheet" type="text/css" />
<title>download</title>
</head>
<%
response.setCharacterEncoding("gb2312");
request.setCharacterEncoding("gb2312");

if (request.getParameter("file") != null) {
OutputStream os = null;
FileInputStream fis = null;
try {
String file = request.getParameter("file");
if (!(new File(file)).exists()) {
System.out.println("没有文件");
return;
}
System.out.println("文件名为:"+file);
os = response.getOutputStream();
response.setHeader("content-disposition", "attachment;filename=" + file);
response.setContentType("application/vnd.ms-excel");//此项内容随文件类型而异
byte temp[] = new byte[1000];
fis = new FileInputStream(file);
int n = 0;
while ((n = fis.read(temp)) != -1) {
os.write(temp, 0, n);
}
} catch (Exception e) {
out.print("出错");
} finally {
if (os != null)
os.close();
if (fis != null)
fis.close();
}
out.clear();
out = pageContext.pushBody();

}
%>

<form action="" method="post">
<select name="file">
<option value="D://test//test.xls">
cccc
</option>
</select>
<input type="submit"/>
</form>
</html>


以上是网上搜索的答案,希望能给你帮助。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧