大小怎么获得的?通过firebug之类的看到的?http请求除了我们常见和常用的get/post以外,head提交方式是仅获得http消息头,在消息头里面再获得content-length对应的消息头数据就是文件大小,你看看是不是以head方式提交的?要下载完整文件要以post方式提交,数据流返回格式默认是html,服务器会自动识别,不过严谨的方式还是手动指定为好
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!-- 导入标签库 --><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->
</head> <body> <hr> <c:forEach items="${map }" var="filename"> ${filename.key }<a href="${pageContext.request.contextPath }/DownloadServlet?filename=${filename.value}">下载</a><br> <%-- ${filename.key }<a href="http://localhost:8080/day19Preview/DownloadServlet?filename=${filename.value}">下载</a><br> --%> </c:forEach> </body></html>
package cx.fileUpload;
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.HashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
/** * Servlet implementation class DownloadServlet */@WebServlet("/DownloadServlet")public class DownloadServlet extends HttpServlet {private static final long serialVersionUID = 1L; /** *@see HttpServlet#HttpServlet() */ public DownloadServlet() { super(); // TODO Auto-generated constructor stub }
/***@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub//response.getWriter().append("Served at: ").append(request.getContextPath());File file =new File(this.getServletContext().getRealPath("WEB-INF/files"));if(request.getParameter("filename" ) != null){//获取参数对应的valueString filename =new String(request.getParameter("filename").getBytes("ISO8859-1"),"UTF-8");int indexOf = filename.lastIndexOf("_");String filename2 = filename.substring(indexOf+1);//反推出文件路径String path = CreateFileName.createFileName(filename2);//拼接成最终下载路径File downFile = new File(file,path+"/"+filename);//输入与输出流FileInputStream fileInputStream = new FileInputStream(downFile);System.out.println(downFile+"downfile++++++++++");//设置响应动作response.setHeader("Content-Type", this.getServletContext().getMimeType(filename2));response.setHeader("Content-Disposition", "attachment;filename="+filename2);int len=0;byte[] bs=new byte[2048];ServletOutputStream outputStream = response.getOutputStream();while((len=fileInputStream.read()) != -1) {outputStream.write(bs, 0, len);}outputStream.flush();outputStream.close();fileInputStream.close();}else { //如果请求中不带参数,则显示所有文件Map<String,String> map =new HashMap<String,String>();//便利出所有文件bianli(file,map);request.setAttribute("map", map);request.getRequestDispatcher("MyJsp.jsp").forward(request, response);}}
/***@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);
}//找出文件中包含的所有文本,将文件路径和名字分别放在map中public void bianli(File file,Map<String, String> map) {File[] listFiles = file.listFiles();for(File fl:listFiles) {if(fl.isDirectory()) {bianli(fl,map);}else {//文件路径String absolutePath = fl.getAbsolutePath();int lastIndexOf = absolutePath.lastIndexOf("/");//包装的文件名String filename = absolutePath.substring(lastIndexOf+1);System.out.println(filename);System.out.println(fl.getName()+"------------");//真正的文件名String nameLast=filename.substring(filename.indexOf("_")+1);map.put(nameLast, fl.getName());}}}}
没有报异常。。
看日志有没有报什么异常。
恩,本人也是初学,练手的
回复你看看 http://takeme.iteye.com/blog/1688273 这篇文章里面的那个copyStream方法,他这样写才是对的,不过他的主旨是一个简单下载功能
看了那个我找到原因了,流操作少传了个参数,大神,3Q
如果是简单的文件下载完全可以用web服务器自带的文件下载功能,好像所有的web服务器都支持文件下载,不需要自己再弄一个了。
还有要指出的一点,客户端下载文件服务器不能简单的像你这个servlet这样弄的,你这样基本是把所有的文件数据写入到输出流后再发送出去的,小文件还行(win上很多情况必须调用flush才会发送数据),大文件就不能这样弄了。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(9)
大小怎么获得的?通过firebug之类的看到的?http请求除了我们常见和常用的get/post以外,head提交方式是仅获得http消息头,在消息头里面再获得content-length对应的消息头数据就是文件大小,你看看是不是以head方式提交的?要下载完整文件要以post方式提交,数据流返回格式默认是html,服务器会自动识别,不过严谨的方式还是手动指定为好
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 导入标签库 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<hr>
<c:forEach items="${map }" var="filename">
${filename.key }<a href="${pageContext.request.contextPath }/DownloadServlet?filename=${filename.value}">下载</a><br>
<%-- ${filename.key }<a href="http://localhost:8080/day19Preview/DownloadServlet?filename=${filename.value}">下载</a><br> --%>
</c:forEach>
</body>
</html>
package cx.fileUpload;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DownloadServlet
*/
@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
*
@see HttpServlet#HttpServlet()
*/
public DownloadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
*
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
File file =new File(this.getServletContext().getRealPath("WEB-INF/files"));
if(request.getParameter("filename" ) != null){
//获取参数对应的value
String filename =new String(request.getParameter("filename").getBytes("ISO8859-1"),"UTF-8");
int indexOf = filename.lastIndexOf("_");
String filename2 = filename.substring(indexOf+1);
//反推出文件路径
String path = CreateFileName.createFileName(filename2);
//拼接成最终下载路径
File downFile = new File(file,path+"/"+filename);
//输入与输出流
FileInputStream fileInputStream = new FileInputStream(downFile);
System.out.println(downFile+"downfile++++++++++");
//设置响应动作
response.setHeader("Content-Type", this.getServletContext().getMimeType(filename2));
response.setHeader("Content-Disposition", "attachment;filename="+filename2);
int len=0;
byte[] bs=new byte[2048];
ServletOutputStream outputStream = response.getOutputStream();
while((len=fileInputStream.read()) != -1) {
outputStream.write(bs, 0, len);
}
outputStream.flush();
outputStream.close();
fileInputStream.close();
}else {
//如果请求中不带参数,则显示所有文件
Map<String,String> map =new HashMap<String,String>();
//便利出所有文件
bianli(file,map);
request.setAttribute("map", map);
request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
}
}
/**
*
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
//找出文件中包含的所有文本,将文件路径和名字分别放在map中
public void bianli(File file,Map<String, String> map) {
File[] listFiles = file.listFiles();
for(File fl:listFiles) {
if(fl.isDirectory()) {
bianli(fl,map);
}else {
//文件路径
String absolutePath = fl.getAbsolutePath();
int lastIndexOf = absolutePath.lastIndexOf("/");
//包装的文件名
String filename = absolutePath.substring(lastIndexOf+1);
System.out.println(filename);
System.out.println(fl.getName()+"------------");
//真正的文件名
String nameLast=filename.substring(filename.indexOf("_")+1);
map.put(nameLast, fl.getName());
}
}
}
}
没有报异常。。
看日志有没有报什么异常。
恩,本人也是初学,练手的
回复
你看看 http://takeme.iteye.com/blog/1688273 这篇文章里面的那个copyStream方法,他这样写才是对的,不过他的主旨是一个简单下载功能
看了那个我找到原因了,流操作少传了个参数,大神,3Q
如果是简单的文件下载完全可以用web服务器自带的文件下载功能,好像所有的web服务器都支持文件下载,不需要自己再弄一个了。
还有要指出的一点,客户端下载文件服务器不能简单的像你这个servlet这样弄的,你这样基本是把所有的文件数据写入到输出流后再发送出去的,小文件还行(win上很多情况必须调用flush才会发送数据),大文件就不能这样弄了。