SpringBoot文件上传控制
来源:互联网 发布:数据属性二元化例子 编辑:程序博客网 时间:2024/05/01 10:03
之前在使用SpringBoot进行文件上传时,遇到了很多问题。于是在翻阅了很多的博文之后,总算将上传功能进行了相应的完善,便在这里记录下来,供自己以后查阅。
首先,是建立一个标准的SpringBoot 的工程,这里使用的IDE是Intellij Idea,为了方便配置,将默认的配置文件替换为了application.yml。
1.在index.html中进行文件上传功能,这里使用的文件上传方式是ajax,当然也可以按照自己的具体要求使用传统的表单文件上传。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>上传测试</title> <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script></head><body> <input id="file" type="file" name="file"/> <br/> <button id="upload" onclick="doUpload()">上传</button> <progress id="progressBar" value="0" max="100"></progress> <script> function doUpload() { var fileObj = document.getElementById("file").files[0]; // js 获取文件对象 var FileController = "/upload"; // 接收上传文件的后台地址 // FormData 对象 var form = new FormData(); form.append("file",fileObj); // XMLHttpRequest 对象 var xhr = new XMLHttpRequest(); //为请求添加返回处理函数 xhr.onreadystatechange=function () { if(this.readyState == 4 && this.status == 200){ var b = this.responseText; if(b == "success"){ alert("上传成功!"); }else{ alert("上传失败!"); } } }; xhr.open("post", FileController, true); //使用进度条记录上传进度 xhr.upload.addEventListener("progress", progressFunction, false); xhr.send(form); } function progressFunction(evt) { var progressBar = document.getElementById("progressBar"); var percentageDiv = document.getElementById("percentage"); if (evt.lengthComputable) { progressBar.max = evt.total; progressBar.value = evt.loaded; percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%"; } } </script></body></html>
2.在MainController添加文件上传的API,并返回上传结果
@PostMapping("/upload") @ResponseBody public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) { String path = "E://upload//"; String fileName = file.getOriginalFilename(); System.out.println(fileName); File targetFile = new File(path); if (!targetFile.exists()) { targetFile.mkdirs(); } File saveFile=new File(path+fileName); // 保存 try { file.transferTo(saveFile); return "success"; } catch (Exception e) { e.printStackTrace(); return "fail"; } }这时,我们进行测试,就可以发现,文件上传已经完成了。
很多时候,我们在进行文件上传时,特别是向普通用户开放文件上传功能时,需要对上传文件的格式进行控制,以防止黑客将病毒脚本上传。单纯的将文件名的类型进行截取的方式非常容易遭到破解,上传者只需要将病毒改换文件名便可以完成上传。
这时候我们可以读取文件的十六进制的文件头,来判断文件真正的格式。
因为我们发现,在我们读取文件的二进制数据并将其转换为十六进制时,同类型文件的文件头数据是相同的,即使改变了其后缀,这个数据也不会改变,例如,png文件的文件头为“89504E47”。
首先,我们将文件的数据进行读取
public class FileUtil { public static String getFileHeader( MultipartFile file) { InputStream is = null; String value = null; try { is = file.getInputStream(); byte[] b = new byte[4]; is.read(b, 0, b.length); value = bytesToHexString(b); } catch (Exception e) { } finally { if (null != is) { try { is.close(); } catch (IOException e) { } } } return value; } private static String bytesToHexString(byte[] src) { StringBuilder builder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } String hv; for (int i = 0; i < src.length; i++) { hv = Integer.toHexString(src[i] & 0xFF).toUpperCase(); if (hv.length() < 2) { builder.append(0); } builder.append(hv); } System.out.println(builder.toString()); return builder.toString(); }}
然后在文件上传的api中进行调用
FileUtil.getFileHeader(file)这时候,我们只需要进行简单的字符串比对,判断调用的返回值是否为“89504E47”,就可以知道上传的是否为png文件。
引用链接
阅读全文
1 0
- SpringBoot文件上传控制
- SpringBoot文件上传
- springboot文件上传
- SpringBoot上传文件
- springboot 文件上传
- SpringBoot之文件上传
- springboot 文件上传
- SpringBoot实现文件上传
- springboot 上传文件
- Springboot 文件上传
- SpringBoot上传文件
- springboot上传文件
- springboot+文件上传和下载
- springboot实现多文件上传
- springboot 文件上传大小配置
- SpringBoot轻松实现文件上传
- 15.springboot实现文件上传
- SpringBoot文件上传和下载
- 304. Range Sum Query 2D
- ArcEngine 报错记录
- 可变参数列表
- CNN-目标检测、定位、分割
- elcipse环境配置及svn插件安装
- SpringBoot文件上传控制
- 使用nexus搭建maven私库并且使用archetype插件创建模板
- 微积分基本定理
- 求第n个斐波拉契数
- 史上最难的一道Java面试题 (分析篇)
- 详细的git GitHub创建管理仓库教程之一
- IO流(10)--带缓冲区的字符流的特有方法
- 读书笔记——深入理解JAVA虚拟机(4)
- 所有的JSP文件全部有红叉