使用COS组件实现文件下载

    技术2022-11-27  63

    完整版见 https://jadyer.github.io/

    这是一个Servlet应用。。

    首先是web.xml

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>DownloadServlet</servlet-name> <servlet-class>com.jadyer.servlet.DownloadServlet</servlet-class> <!-- 指定所要下载的文件,在本地服务器上的存储位置 --> <init-param> <param-name>filePath</param-name> <param-value>D://mydata//upload</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DownloadServlet</servlet-name> <url-pattern>/servlet/DownloadServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>download.jsp</welcome-file> </welcome-file-list> </web-app>

    然后是为用户提供下载链接的download.jsp页面

    <%@ page language="java" pageEncoding="UTF-8"%> <a href="<%=request.getContextPath()%>/servlet/DownloadServlet?fileName=春天里1295154365000.mp3">点击下载歌曲:《春天里》</a> <%-- 这里的fileName参数,是以GET方式发送的,并且fileName的值对应的是服务器上的文件名称 也就是在web.xml中配置的filePath参数所指定的服务器上的该文件目录下所保存的文件名称 也就是说,此时的fileName指定的是已经被上传到服务器上的文件名,而非上传之前的文件名 --%>

    最后是用来处理文件下载的核心Servlet

    package com.jadyer.servlet; import java.io.IOException; import java.net.URLEncoder; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.oreilly.servlet.ServletUtils; /** * 关于Struts2与COS * @see Struts2可能是由于License的原因,貌似不支持COS上传 * @see 我们可以到Struts2的官网http://struts.apache.org/2.2.1.1/index.html查找一下 * @see 点击Struts2首页左侧的FAQs,然后在新展开的页面中点击How do we upload files * @see 接着在新展开页面中点击See the file upload page链接 * @see 此时就会打开http://struts.apache.org/2.2.1.1/docs/file-upload.html页面 * @see 在该页面的最下方我们会看到,下面的一段话 * @see There was a third alternative, cos, but it was removed due to licensing incompatibilities * @see 不过,已经有人尝试解决它了,也就是实现org.apache.struts2.dispatcher.multipart.MultiPartRequest接口 * @see 然后在MultiPartRequest接口的实现类中,进行相应的处理,详情请参见http://www.javaeye.com/topic/316626 */ public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = -5675762049168848765L; private String filePath; public void init(ServletConfig config) { //取得web.xml中配置的filePath参数的值 filePath = config.getInitParameter("filePath"); } public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { this.doPost(request, response); } /** * 此时需要在该Web项目中引入cos.jar * 可通过http://www.servlets.com/cos/cos-26Dec2008.zip下载 */ protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { String fileName = request.getParameter("fileName"); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); //采用COS提供的ServletUtils类完成文件下载 //在ServletUtils中一共提供了7个静态方法,可以实现不同场景的文件下载以及其它需求 //其中使用returnFile()可以下载本地的文件,使用returnURL()可以下载网络上的文件 //ServletUtils.returnURL(new URL("http://29.duote.org/javadmscgj.exe"), response.getOutputStream()); ServletUtils.returnFile(filePath + "//" + fileName, response.getOutputStream()); } }

    最新回复(0)