1、假如我们将架构分为dao层、service层和controller层。
- 此时我们将文件保存到服务器的磁盘中存储,所以基本上不需要对数据库进行操作,只需要在磁盘上进行io操作即可。因此基本上就没有了dao层的事情了。为了简化代码,若存在对dao层操作的代码,下文中将会简单介绍而不全部写出。
- 实现service层,我们需要封装service层的方法,同时spring考虑的很周到,为我们提供了一个专门用来文件下载的类:
org.springframework.http.ResponseEntity
。
- 我们目前需要做的就是,通过这个现成的类,来对其再封装,封装成我们需要的样子。
- 附带springframework的api文档:
https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/javadoc-api/
2、上代码(service层)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package top.BinGCU.service;
import org.springframework.http.ResponseEntity; import java.io.IOException;
public interface ResourcesParsingService {
ResponseEntity<byte[]> obtainAvatarReponseEntity(String downloadFileName, String realPath ) throws IOException;
String queryAvatarPathByUserAccountStr( String userAccountStr); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package top.BinGCU.service.Impl;
import org.apache.commons.io.FileUtils; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import top.BinGCU.Util.ApplicationContextShortcut; import top.BinGCU.dao.UserMapper; import top.BinGCU.service.ResourcesParsingService;
import java.io.File; import java.io.IOException;
public class ResourcesParsingServiceImpl implements ResourcesParsingService { @Override public ResponseEntity<byte[]> obtainAvatarReponseEntity(String downloadFileName, String realPath ) throws IOException { File file = new File(realPath); System.out.println("Check file real path: "+file);
HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", downloadFileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }
}
|
3、上代码(controller层)
比如我这里的FileLoadController中的其中一个url就是用来下载用户的头像图片:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package top.BinGCU.controller;
import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import top.BinGCU.service.Impl.ResourcesParsingServiceImpl; import top.BinGCU.service.ResourcesParsingService;
import javax.servlet.http.HttpServletRequest; import java.io.IOException;
@Controller public class FileLoadController { private static String sessionName = "userAccountStr"; private ResourcesParsingService resourcesParsingService = new ResourcesParsingServiceImpl();
@RequestMapping("/fileLoader/avatar/download") @ResponseBody public ResponseEntity<byte[]> avatarDownload(HttpServletRequest request) throws IOException { String userAccountStr = (String) request.getSession().getAttribute(sessionName); String realPath = resourcesParsingService.queryAvatarPathByUserAccountStr(userAccountStr); String downName = realPath.substring( request.getServletContext().getRealPath("/WEB-INF/static/img/avatar").length(), realPath.length() );
System.out.println("downloadFileName:"+downName);
return resourcesParsingService.obtainAvatarReponseEntity(downName, realPath); } }
|
为了简洁,去掉注释和多于没用的信息后的效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Controller public class FileLoadController { private static String sessionName = "userAccountStr"; private ResourcesParsingService resourcesParsingService = new ResourcesParsingServiceImpl();
@RequestMapping("/fileLoader/avatar/download") @ResponseBody public ResponseEntity<byte[]> avatarDownload(HttpServletRequest request) throws IOException {
String userAccountStr = (String) request.getSession().getAttribute(sessionName);
String realPath = resourcesParsingService.queryAvatarPathByUserAccountStr(userAccountStr); String downName = realPath.substring( request.getServletContext().getRealPath("/WEB-INF/static/img/avatar").length(), realPath.length() );
return resourcesParsingService.obtainAvatarReponseEntity(downName, realPath); } }
|