java实现从远程tomcat服务器下载文件到本地 tomcat 发布项目 test的 webroot/upload目录下有文件 test.xml 现在要从java中实现 从服务器目录下把文件下载到本机上, 本人刚入行, 求帮助代码怎么实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <%@ 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> 以上是网上搜索的答案,希望能给你帮助。 如何在.net页面中调用JAVA程序 tomcat域名配置 struts2 标签 集合过滤的问题 高效率拼接字符串 JSP文件下载 radio怎么获得值阿?????????????? 怎么控制<td>间字符的个数阿 Apache POI 问题! 急! 请教关于用jsp实现UBB标签的例子!!!! servlet的配置问题,我安装了jdk1.3 诚心求助:关于页面回显的问题 求指点,hibernate事务不起作用
<%@ 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>
以上是网上搜索的答案,希望能给你帮助。