setExt(extension).setSize(String.valueOf(size)).setType(type).
setPath("/files/" + date).setUserId(user.getId());
System.out.println(userFile);
userFileService.save(userFile);
return "redirect:/file/showAll";
}
[](https://gitee.com/vip204888/java-p7)文件上传测试
-------------------------------------------------------------------------
之前我们完成了展示所有文件功能,但是由于数据库中没有数据,因此显示的页面为空,现在我们利用文件上传功能往数据库中添加数据。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200514184544474.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNDA5NQ==,size_16,color_FFFFFF,t_70)
点击 **选择文件**,选择电脑上的任意文件,然后点击 **上传文件**。
点击上传文件后页面自动再次跳到显示所有页面,此时可以看到页面上已经有数据了。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200514185051776.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNDA5NQ==,size_16,color_FFFFFF,t_70)
[](https://gitee.com/vip204888/java-p7)文件下载与在线打开
============================================================================
`com.yusael.dao` 包下:`UserFileDAO.java` 接口中增加一个方法:
// 根据id找到文件信息
UserFile findById(Integer id);
// 更新下载次数
void update(UserFile userFile);
`resources/com/yusael/mapper` 目录下:`UserFileDAOMapper.xml` 中增加代码:
select id,oldFileName,newFileName,ext,path,size,type,isImg,downcounts,uploadTime,userId
from t_files
where id = #{id}
update t_files set downcounts = #{downcounts} where id = #{id}
* * *
`com.yusael.service` 包下:`UserFileService.java` 接口中增加方法:
UserFile findById(Integer id);
void update(UserFile userFile);
`com.yusael.service` 包下:`UserFileServiceImpl.java` 实现类中增加代码:
@Override
public UserFile findById(Integer id) {
return userFileDAO.findById(id);
}
@Override
public void update(UserFile userFile) {
userFileDAO.update(userFile);
}
* * *
[](https://gitee.com/vip204888/java-p7)文件下载、在线打开核心 UserFileController
-------------------------------------------------------------------------------------------------
`com.yusael.controller` 包下:`UserFileController.java` 中增加方法:
// 文件下载
@GetMapping("/download")
public void download(Integer id, String openStyle, HttpServletResponse response) throws IOException {
// attachement是以附件形式下载, inline是在线打开
openStyle = "inline".equals(openStyle) ? "inline" : "attachment";
// 获取文件信息
UserFile userFile = userFileService.findById(id);
if ("attachment".equals(openStyle)) {
// 更新下载次数
userFile.setDowncounts(userFile.getDowncounts() + 1); // 下载次数+1
userFileService.update(userFile);
}
// 根据 文件信息中文件名字 和 文件存储路径 获取文件真实路径
String realPath = ResourceUtils.getURL("classpath:").getPath() + "/static" + userFile.getPath();
// 获取文件输入流
InputStream is = new FileInputStream(new File(realPath, userFile.getNewFileName()));
// 获取响应输出流
response.setHeader("content-disposition", openStyle + ";fileName=" + URLEncoder.encode(userFile.getOldFileName(), "UTF-8"));
ServletOutputStream os = response.getOutputStream();
// 文件拷贝
IOUtils.copy(is, os);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
[](https://gitee.com/vip204888/java-p7)文件下载、在线打开测试
------------------------------------------------------------------------------
前面我们已经完成了文件的上传功能,经过上传了图片,现在数据库中已经有了几张图片信息,我们点击下载,可以跳出下载界面,并且文件的下载次数会 + 1。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200514194803650.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNDA5NQ==,size_16,color_FFFFFF,t_70)
点击在线打开,如果是图片格式的文件,则会直接打开,否则依旧是以附件的形式下载,但是不增加下次次数。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200514194928904.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczNDA5NQ==,size_16,color_FFFFFF,t_70)
[](https://gitee.com/vip204888/java-p7)文件删除
=======================================================================
`com.yusael.dao` 包下:`UserFileDAO.java` 接口中增加一个方法:
// 删除数据库中的文件
void delete(Integer id);
`resources/com/yusael/mapper` 目录下:`UserFileDAOMapper.xml` 中增加代码:
delete from t_files
where id = #{id}
* * *
`com.yusael.service` 包下:`UserFileService.java` 接口中增加方法:
void delete(Integer id);
`com.yusael.service` 包下:`UserFileServiceImpl.java` 实现类中增加代码:
@Override
public void delete(Integer id) {
userFileDAO.delete(id);
}
* * *
`com.yusae.controller` 包下:`UserFileController.java` 中增加方法:
// 文件删除
@GetMapping("/delete")
public String delete(Integer id) throws FileNotFoundException {
// 根据id查询信息
UserFile userFile = userFileService.findById(id);
// 删除服务器上的文件
// 通过相对路径获取绝对路径
String realPath = ResourceUtils.getURL("classpath:").getPath() + "/static" + userFile.getPath();
File file = new File(realPath + "/" + userFile.getNewFileName());
if (file.exists()) file.delete(); // 如果服务器上文件存在则删除文件
// 删除数据库中的文件
userFileService.delete(id);
那么如何才能正确的掌握Redis呢?
为了让大家能够在Redis上能够加深,所以这次给大家准备了一些Redis的学习资料,还有一些大厂的面试题,包括以下这些面试题
-
并发编程面试题汇总
-
JVM面试题汇总
-
Netty常被问到的那些面试题汇总
-
Tomcat面试题整理汇总
-
Mysql面试题汇总
-
Spring源码深度解析
-
Mybatis常见面试题汇总
-
Nginx那些面试题汇总
-
Zookeeper面试题汇总
-
RabbitMQ常见面试题汇总
JVM常频面试:
Mysql面试题汇总(一)
Mysql面试题汇总(二)
Redis常见面试题汇总(300+题)
有需要的朋友,可以直接点击这里免费获取
-
Nginx那些面试题汇总
-
Zookeeper面试题汇总
-
RabbitMQ常见面试题汇总
JVM常频面试:
[外链图片转存中…(img-qyyvOJyZ-1628419364946)]
Mysql面试题汇总(一)
[外链图片转存中…(img-Q5bRi63R-1628419364948)]
Mysql面试题汇总(二)
[外链图片转存中…(img-bwSTsi95-1628419364950)]
Redis常见面试题汇总(300+题)
[外链图片转存中…(img-Zwwab5VH-1628419364951)]
有需要的朋友,可以直接点击这里免费获取
绝无套路!!