文件上传

1.需求

1.1 文件需要在fastDfs,和本地各存一份 ,并且返回本地路径点击保存时将路径存入数据库
1.2 文件下载时先从本地拉取,如果本地文件不存在,从fastDfs拉取

2.效果展示


3.前段代码

3.1.0 合同签订框-基本信息
<el-row><el-form-item label="附件上传:" prop="remark" size="mini" ><el-uploadclass="upload-demo"ref="uploadFileId" //保存返回的路径时使用:action="uploadFileUrl"  //必选参数,上传的地址:on-preview="handlePreview"  //点击文件列表中已上传的文件时的钩子:on-success="handleSuccess"    //文件上传成功时的钩子:on-remove="handleRemove" //文件列表移除文件时的钩子:before-upload="beforeUpload" //上传文件之前的钩子:multiple =false //是否支持多选文件:limit="1"accept=".pdf":file-list="fileList" //上传的文件列表:auto-upload="autoUpload"> //是否在选取文件后立即进行上传<el-button size="small" type="primary">点击上传</el-button><span>上传pdf文件,不超过20M</span></el-upload></el-form-item>
</el-row>
3.1.1 合同签订框-合同签订
<el-table-column prop="filePath" label="附件" align="center"show-overflow-tooltip><template slot-scope="scope"><span v-if=" scope.row.filePath != null && scope.row.filePath != '' " style="color:#FF5722;"><el-link  size="mini" @click="previewFile(scope.row.filePath)">预览<i class="el-icon-view el-icon--right"></i> {{scope.row.filePath.split("/")[2]}}</el-link></span></template>
</el-table-column>
3.2 JS中需要定义的参数
uploadFileUrl: contextPath + '/api/apitudeFile/uploadSingleFile',
fileList: [],
autoUpload:false,
3.3 methods方法
//附件上传成功后的方法
handleSuccess:function(response, file, fileList){vm.ruleForm.filePath = response.toString();
},//移除附件列表
handleRemove:function(file, fileList){if(isNotNULL(vm.ruleForm.id)){vm.ruleForm.filePath=null;let param ={id:vm.ruleForm.id};this.$resource(contextPath + '/api/hrpEmployeeContract/deleteFilePath').query(param).then(function (response) {this.search();this.$message.warning("文件移除成功");}).catch(function (error) {this.showMsgError(error.data.errMsg);});}
},//点击附件的方法
handlePreview:function(file){this.previewFile(file.url);
},//预览文件
previewFile(filePath){window.top.vueObject.openHelp(contextPath + '/api/hrpEmployeeContract/downloadFile?filePath=' + filePath,'劳动合同预览');
},//文件上传前的校验
beforeUpload:function(file){let name = file.name;let size = file.size / 1024 / 1024;debuggerif(!(name.indexOf("pdf")>0)  && !(name.indexOf("PDF")>0)){this.$message.warning("请选择pdf文件");return false;}if(size > 20 ){this.$message.warning("文件大小不要过20M");return false;}
},

4 后台代码

4.1 文件上传后台
4.1.0 Controller
@RequestMapping("/uploadSingleFile")
public ResponseEntity<Object> uploadSingleFile(@RequestParam("file") MultipartFile multipartFile,HttpServletRequest request ){String path =  request.getSession().getServletContext().getRealPath("/upload");String filePath =  mmsApitudeFileApi.uploadSingleFile(multipartFile,path);return new ResponseEntity<Object>(filePath,HttpStatus.OK);
}
4.1.1 ServiceImp
@Override
public String uploadSingleFile(MultipartFile file, String realPath) {try {//保存时的文件名String  originalFileName = file.getOriginalFilename();String type = StringUtils.substringAfterLast(originalFileName,".");String dateName = System.currentTimeMillis() +"_"+ originalFileName;//保存文件的绝对路径File dir = new File(realPath);if (!dir.exists()) {dir.mkdirs();}String pathToDb = "/upload/" + dateName;MmsApitudeFile mmsApitudeFile = new MmsApitudeFile();mmsApitudeFile.generateId();mmsApitudeFile.setFileStatus("1");//fileType字段存储业务主键mmsApitudeFile.setFileType("");mmsApitudeFile.setFileText(file.getBytes());mmsApitudeFile.setFileSize(new BigDecimal(file.getSize()));mmsApitudeFile.setFileForm(type);mmsApitudeFile.setFileName(dateName);mmsApitudeFile.setFileUploadTime(new Date());mmsApitudeFile.setFileUrl(pathToDb);String filePath = realPath + File.separator + dateName;File tempFile = new File(filePath);file.transferTo(tempFile);mmsApitudeFileDao.insert(mmsApitudeFile);return   pathToDb;}catch (Exception e ){throw new RuntimeException(e);}
}
4.2 文件预览,下载
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET,produces="application/pdf")
public ResponseEntity<Object> downloadFile(String filePath, HttpServletRequest request, HttpServletResponse response) {Assert.notBlank("filePath", "文件路径不可为空");String path = request.getSession().getServletContext().getRealPath("");File file = new File(path + filePath);if (!file.exists()) {MmsApitudeFile mmsApitudeFile = new MmsApitudeFile();mmsApitudeFile.setFileUrl(filePath);List<MmsApitudeFile> pictureList = mmsApitudeFileApi.findList(mmsApitudeFile);mmsApitudeFileService.downloadFileIfNotExists(pictureList, path);}try {String name = StringUtils.substringAfterLast(filePath, "/");HttpHeaders headers = new HttpHeaders();response.addHeader("Content-Disposition", "inline;filename="+new String(name.getBytes(StandardCharsets.UTF_8),"iso8859-1"));return new ResponseEntity<Object>(FileUtil.readBytes(new File(path + filePath)), headers, HttpStatus.OK);} catch (Exception e) {throw  new RuntimeException(e);}}

文件上传(人事信息管理-劳动合同)相关推荐

  1. ajax请求携带tooken_Spring Boot+Vue 文件上传,如何携带令牌信息?

    关于文件上传这块,松哥之前也写了好几篇文章了,甚至还有视频: Spring Boot+Vue+FastDFS 实现前后端分离文件上传 但是,之前和小伙伴们提到的方案,是基于 session 来做认证的 ...

  2. 基于Python Flask框架+jquery Ajax技术实现的增删改查(CRUD)+Ajax的异步文件上传

    运行界面(话不多说先上图) 运行之后的index界面,有登陆.注册功能 登陆界面,输入数据库中用户名.密码不为空且密码是加密的数据,进入main界面 注册界面,用的bootstrop的弹窗,用户名和密 ...

  3. springboot设置文件上传大小(tomcat默认1M)

    application.yml # 设置文件上传大小(tomcat默认1M) server:tomcat:max-http-form-post-size: -1 spring:servlet:mult ...

  4. 将文件上传至ftp服务器,FTP文件上传工具类,将文件上传至服务器指定目录

    将文件上传至ftp服务器,传入File对象,将文件上传至ftp服务器 需要配置修改的点: 1. 服务器ip端口(服务器ip 端口22/21). 2. 服务器账号密码(服务器登录用户名密码). 3. 上 ...

  5. Springboot 多文件上传

    其实多个文件和单个文件上传是一样的,可以使用同一个Controller 添加依赖 <!-- https://mvnrepository.com/artifact/commons-fileuplo ...

  6. django文件上传

    Django在处理文件上传时,文件数据被打包封装在request.FILES中. 一.简单上传 首先,写一个form模型,它必须包含一个FileField: # forms.py from djang ...

  7. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

  8. 模拟文件上传(一):手动文件上传

    关于上传文件,首先我的第一个案例是一个文本文件的上传,简单容易上手! 首先我们上传文件肯定就属于实体内容部分了:所以不能过GET方式请求了,要通过POST方式请求: 因为: 1.get方式是URL传值 ...

  9. Java中的文件上传2(Commons FileUpload:commons-fileupload.jar)

    相比上一篇使用Servlet原始去实现的文件上传(http://www.cnblogs.com/EasonJim/p/6554669.html),使用组件去实现相对来说功能更多,省去了很多需要配置和处 ...

最新文章

  1. java和python哪个好就业2020-JAVA和Python哪个好就业?
  2. MySQL服务器学习笔记!(二) ——数据库各项操作
  3. 对Python中路径操作指南
  4. 仿Office的程序载入窗体
  5. 工业交换机和工业级光纤收发器的区别
  6. java中 queryparam_java – 何时使用@QueryParam和@PathParam
  7. Windows 2012 英文版系统安装中文语言包及时间格式设置
  8. java urlrewrite_Java|urlrewrite|URL重写|多个参数
  9. mysql 查询一年中每个月份的数据量
  10. Unity性能优化之物理引擎的优化(三)
  11. 数字电路设计之低功耗设计方法六:旁路(by-passing)
  12. 计算机 不识u盘,电脑uefi不识别u盘怎么办
  13. Android VideoView播放 项目中的 视频文件 自动横屏 全屏播放
  14. Downloading https://ultralytics.com/assets/Arial.ttf to /data/..../.config/Ultralytics/Arial.ttf
  15. 【开源】STC单片机免冷启动自动下载器
  16. 真 OO无双之真乱舞书 序言
  17. Caffe 代码解读之全连接层 inner product layer
  18. Unity WebView 插件⭐️(十三)特定模块 iOS网页视图—iOSWebViewWebView
  19. 微服务网关soul,这篇值得你拥有
  20. Android 大姨妈、经期日历,美柚经期效果

热门文章

  1. js网页判断移动终端浏览器版本信息是安卓还是苹果ios,判断在微信浏览器跳转不同页面,生成二维码...
  2. 求个位、十位、百位上的数字
  3. Android 快速实现新手引导层的库,通过简洁链式调用,一行代码实现引导层的显示
  4. oracle marketing job description,职场英语:销售总监英文简历
  5. 互联网晚报 | 9月17日 星期五 | 天猫超市推出“异地订单”优先配送服务;荣耀与微软达成战略合作;百度昆仑芯落地已超2万片...
  6. 认识区块链,认知区块链— —数据上链
  7. 机载雷达导论(第7~9章)
  8. PT_中心极限定理CLT:棣莫佛-拉普拉斯定理de Moivre - Laplace CLT+林德伯格-列维(Lindeberg-Levy)定理
  9. Flutter传感器
  10. JS canvas实现图片显示