采用jspSmartUpload组件进行文件的下载
2012-10-23 21:42
573 查看
刚才讲了上传的功能,现在讲讲文件的下载功能吧......
1.先导入jspsmartUpload的一个包
2.jsp中的代码
3.Downservlet中的java代码
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 } } }
相关文章推荐
- 使用jspSmartUpload组件进行文件上传、下载
- 采用jspSmartUpload组件进行文件的上传
- JSP中使用jspsmartupload组件进行文件的上传和下载
- 采用jspSmartUpload组件进行文件的上传
- 使用jspSmartUpload组件进行文件上传、下载
- 使用jspSmartUpload组件进行文件上传、下载、批量下载
- 使用jspSmartUpload组件进行文件上传、下载
- 使用jspSmartUpload组件进行文件上传、下载
- 在项目中使用SmartUpload组件可以进行文件的上传和下载操作
- jsp使用jspSmartUpLoad组件实现中文文件下载
- 搞定使用jspsmartupload 组件进行上传和下载出现的乱码问题。
- 使用smartupload组件实现jsp+jdbc上传下载文件实例解析
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(一):JspSmart之upload组件源码及使用
- JSP 文件 下载 代码 (不使用 jsp smart upload 组件) 解决中文名 问题
- Servlet.service() for servlet jsp threw exception java.lang.Il在jsp中使用jspsmartupload组件下载文件时将抛出异常的解决方案
- 关于使用jspsmartupload.jar进行文件的上传与下载
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- 利用jspsmartupload组件来上传和下载文件
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- jspSmartUpload--文件上传与下载组件的应用----文件上传、下载、删除