此篇是接上篇《文件上传服务器(附源码)》点击可跳转,均使用NIO。写源码如何上传服务器的从服务器下载文件的Demo,均已测试可以运行,本人不太会写文章,可能因为头条的编辑器问题,无法有漂亮的排版,但是有详细的代码说明,粘贴即可使用。在此对文件上传及下载需要的jar做一个简单的总结:
文件上传的文章是使用的jsee目录结构,文件下载使用的是maven目录结构,只是对jar包的添加方式不同而已。网上有很多说明,在此不多说
需要jar包:commons-fileupload-1.3.2,commons-io-2.5,jstl-1.2
本项目是采用的maven目录结构,但是粘贴代码即可使用,请关注下文中涉及的目录
1.web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>efg</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>fileUp</servlet-name> <servlet-class>com.abc.util.UploadHandleServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>fileUp</servlet-name> <url-pattern>/fileup.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>listFile</servlet-name> <servlet-class>com.abc.util.ListFileServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>listFile</servlet-name> <url-pattern>/listFile.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>downLoadServlet</servlet-name> <servlet-class>com.abc.util.DownLoadServlet1</servlet-class> </servlet> <servlet-mapping> <servlet-name>downLoadServlet</servlet-name> <url-pattern>/downLoadServlet.do</url-pattern> </servlet-mapping></web-app>2.listfile.jsp 显示下载目录中
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html><html><head><meta charset="UTF-8"><title>文件下载</title></head><body> <c:forEach var= "me" items="${fileMap }"> <c:url value="/downLoadServlet.do" var= "downurl"> <c:param name="filename" value="${me.key }"></c:param> </c:url> ${me.value }:<a href="${downurl }">下载</a><br> </c:forEach></body></html>3.maven项目中pom.xml -导入项目jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.abc</groupId> <artifactId>efg</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies></project>4.ListFileServlet -获取现在路径下的文件
package com.abc.util;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ListFileServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uploadFilePath = this.getServletContext().getRealPath("/WEB-INF/upload"); Map<String, String> fileMap = new HashMap<String, String>(); File filePath = new File(uploadFilePath); System.out.println(uploadFilePath); if (!filePath.exists()) { req.setAttribute("message", "文件为空源码如何上传服务器!"); req.getRequestDispatcher("/WEB-INF/jsp/message.jsp").forward(req, resp); } else { fileList(filePath, fileMap); req.setAttribute("fileMap", fileMap); req.getRequestDispatcher("/WEB-INF/jsp/listfile.jsp").forward(req, resp); } } private void fileList(File file, Map<String, String> fileMap) { if(!file.isFile()) { File[] files = file.listFiles(); for(File file2 : files) { fileList(file2,fileMap); } }else { System.out.println("==="); String realName = file.getName().substring(file.getName().lastIndexOf("_")+1); System.out.println(realName); System.out.println(file.getName().substring(file.getName().indexOf("_")+1)); System.out.println("==="); fileMap.put(file.getName(), realName); } }}5.DownLoadServlet 文件下载工具类
package com.abc.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DownLoadServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); String fileName = req.getParameter("filename"); System.out.println(fileName); String fileSaveRootPath = this.getServletContext().getRealPath("/WEB-INF/upload"); String realName = fileName.substring(fileName.indexOf("_") + 1); String path = findFileSavePathByFileName(fileName, fileSaveRootPath); File file = new File(path + File.separator + fileName); if (!file.exists()) { req.setAttribute("message", "您要下载的资源已被删除!"); req.getRequestDispatcher("/WEN-INF/jsp/message.jsp").forward(req, resp); return; } resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realName, "utf-8")); FileInputStream in = new FileInputStream(path + File.separator + fileName); OutputStream os = resp.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) > 0) { os.write(buf, 0, len); } in.close(); os.close(); } private String findFileSavePathByFileName(String fileName, String fileSaveRootPath) { int hashcode = fileName.hashCode(); int dir1 = hashcode & 0xf; int dir2 = (hashcode & 0xf0) >> 4; String dir = fileSaveRootPath + "\\" + dir1 + "\\" + dir2; System.out.println(dir); System.out.println(fileSaveRootPath + File.separator + dir1 + File.separator + dir2); return dir; }}文件上传与下载参照此两篇文章即可实现,至于细节各位可根据自己的项目进行调整,如有好的建议欢迎分享。本人将分享一些在工作项目中实际使用的工具类,烦请各位路过大佬提出改进建议。谢谢源码如何上传服务器! 海报
0 条评论
39
相关文章
本站已关闭游客评论,请登录或者注册后再评论吧~