Spring MVC实现文件下载
文件下载的含义十分简单,它指的就是将服务器中的文件下载到本机上。
一、在之前Spring MVC文件上传中和跨服务器保存文件 项目中操作
在WEB-INF/templates下创建一个download.html 的代码,在图片旁边添加一个文件下载的超链接代码如下:
<!DOCTYPE html> <html lang="en"> <head> <title>Title</title> <style> #playerTable{ width: 50%; border: 3px solid cadetblue; margin: 0px auto; text-align: center; } #playerTable th,td{ border: 1px solid gray; } #playerTable img{ width: 100px; height: 100px; } </style> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript"></script> <script> $(function(){ $.ajax({ type:"get", url:"getAllPlayer",//请求后端的地址,获取查出来的结果 success:function(players){ $.each(players,function(i,e){ $("#playerTable").append('<tr>\n' + ' <td>'+e.id+'</td>\n' + ' <td>'+e.username+'</td>\n' + ' <td>'+e.password+'</td>\n' + ' <td>'+e.nickname+'</td>\n' + ' <td>\n' + ' <img src="http://192.168.141.134:8080/upload/'+e.photo+'" alt="" src>\n' + ' </td>\n' + ' <td>\n' + ' <a href="fileDownload.do?photo='+e.photo+'&filetype='+e.filetype+'">下载</a>\n' + ' </td>\n' + ' </tr>') }) } }) }) </script> </head> <body> <table id="playerTable" cellspacing="0xp" cellpadding="0px"> <tr> <th>编号</th> <th>用户名</th> <th>密码</th> <th>昵称</th> <th>头像</th> <th>操作</th> </tr> </table> </body> </html>
二、在mapper层进行操作
在PlayerMapper中添加如下内容:
package com.augus.mapper; import com.augus.pojo.Player; import java.util.List; public interface PlayerMapper { int addUser(Player player);
//查询出表中所有的信息 List<Player> getAllPlayer(); }
在PlayerMapper.xml中添加如下内容:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.augus.mapper.PlayerMapper"> <insert id="addUser"> insert into player values(DEFAULT ,#{username},#{password},#{nickname},#{photo},#{filetype}) </insert> <!--查询表中所有的数据--> <select id="getAllPlayer" resultType="Player"> select * from player </select> </mapper>
三、在service层进行操作
在PlayerService添加获取所有信息的接口
package com.augus.service; import com.augus.pojo.Player; import java.util.List; public interface PlayerService { int addUser(Player player); List<Player> getAllPlayer(); }
在PlayerService添加获取所有信息的接口
package com.augus.service.impl; import com.augus.mapper.PlayerMapper; import com.augus.pojo.Player; import com.augus.service.PlayerService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class PlayerServiceImpl implements PlayerService { //这个本来@Autowired保存,所以改用@Resource @Resource private PlayerMapper playerMapper; @Override public int addUser(Player player) { return playerMapper.addUser(player); } //列出所有的数据 @Override public List<Player> getAllPlayer() { return playerMapper.getAllPlayer(); } }
四、在controller层进行操作
在FileUploadController 添加一下代码实现文件下载
package com.augus.controller; import com.augus.pojo.Player; import com.augus.service.PlayerService; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @Controller public class FileUploadController { //文件存储位置,定为私有不可修改的静态属性 private final static String FILESERVER = "http://192.168.141.134:8080/upload/"; @Autowired private PlayerService playerService;
//打开文件下载页面 @RequestMapping("/playerAllList") public String playerAllList(){ return "download"; } //异步请求会调用这个地址,将从数据库查出的数据返回给前端页面 @RequestMapping("/getAllPlayer") @ResponseBody public List<Player> getAllPlayer(){ return playerService.getAllPlayer(); } //实现下载文件 @RequestMapping("/fileDownload.do") public void fileDownLoad(String photo, String fileType, HttpServletResponse response) throws IOException { //设置响应头 // 指定浏览器下载下的数据保存到磁盘上面,不要在浏览器之间解析 response.setHeader("Content-Disposition","attachment;filename="+photo); //告诉浏览器下载的文件类型 response.setContentType(fileType); //获取一个文件的输入流 InputStream inputStream = new URL(FILESERVER + photo).openStream(); //获取一个指向浏览器的输出流 ServletOutputStream outputStream = response.getOutputStream(); //向浏览器响应文件即可 IOUtils.copy(inputStream,outputStream); } }
五、重新部署Tomcat
访问地址http://localhost:8080/upfile_war_exploded/playerAllList,如下: