您的位置:首页 > Web前端 > JavaScript

采用jspSmartUpload组件进行文件的下载

2012-10-23 21:42 573 查看
                       刚才讲了上传的功能,现在讲讲文件的下载功能吧......

  

1.先导入jspsmartUpload的一个包

2.jsp中的代码

  

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@page import="java.io.File"%>
<%
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 'down.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>
<%
String url=super.getServletContext().getRealPath("/")+"imges";

File f=new File(url);//新建一个对象

File[] files=f.listFiles();//得到所在目录下的所有的文件

for(File file:files){

out.print("<a href='Downservlet?fileName="+file.getName()+"'>"+file.getName()+"</a><br>");
}
%>
</body>
</html>


3.Downservlet中的java代码

  

package com.zuxia.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;

public class Downservlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String fileName=request.getParameter("fileName");

//Get方式设置编码的格式
fileName=new String(fileName.getBytes("ISO-8859-1"),"GB2312");

//第一步:创建对象
SmartUpload smart=new SmartUpload();

smart.initialize(super.getServletConfig(), request, response);//初始化

//第三步:设置浏览器的行为(始终以附件的形式处理文件)
smart.setContentDisposition(null);

try {

//第四部:将文件下到指定的位置
smart.downloadFile("/imges/"+fileName);

System.out.println("ok");
} catch (Exception e) {
// TODO: handle exception
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐