springBoot-文件上传下载

  • springboot  文件上传接口
    @RequestMapping(value = "upload",method = {RequestMethod.POST})
    public Result uploadFile(@RequestParam MultipartFile file,@RequestParam String type,@RequestParam String upload_user){
        try {
            if(StringUtils.isEmpty(type)){
            	return BuildResult.buildOutResult(ResultEnum.FAIL,"文件类型不能为空");
            }
            if ("image,video,voice".indexOf(type.toLowerCase()) < 0) {
            	return BuildResult.buildOutResult(ResultEnum.FAIL,"文件类型不合法");
            }
            if(file.isEmpty()){
            	return BuildResult.buildOutResult(ResultEnum.FAIL,"文件不能为空");
            }

            //记录上传过程起始时的时间,用来计算上传时间
            Long pre = (long) System.currentTimeMillis();
            Result re = this.wsFileService.saveWsFile(file,upload_user,type);
            //记录上传该文件后的时间
            Long finaltime = (long) System.currentTimeMillis();
            LogUtil.info("uploadFile:" + (finaltime - pre) + "ms");
            return re;
        } catch (Exception e) {
        	LogUtil.error(e.getMessage(),e);
            return BuildResult.buildOutResult(ResultEnum.FAIL,"上传失败");
        }
    }
	public Result saveWsFile(MultipartFile file,String upload_user,String type) throws RuntimeException{
		
        String originalFilename = file.getOriginalFilename();
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
        
        String uuid = UUIDUtil.getUUID32();
        //检测文件类型以及大小是否符合要求
        long size = file.getSize();
        
		//FileCopyUtils.copy(new File(file.getInputStream()), new File(filePath + "/" + uuid + "." + fileSuffix));
        try {
        	File dest = new File(filePath + "/" + uuid + "." + fileSuffix);
			
			ChatFileInfo fileinfo = new ChatFileInfo();
			fileinfo.setDeleted("0");
			fileinfo.setFileId(uuid);
			fileinfo.setFileName(originalFilename);
			fileinfo.setFileSuffix(fileSuffix);
			fileinfo.setFileLength(String.valueOf(size));
			fileinfo.setFilePath(dest.getAbsolutePath());
			fileinfo.setCreateTime(new Date());
			fileinfo.setUpdateTime(fileinfo.getCreateTime());
			fileinfo.setUploadUser(upload_user);
			fileinfo = this.wsfileDao.saveWsFile(fileinfo);
			file.transferTo(dest);
			
			Map<String, Object> resmap = new HashMap<String, Object>();
	        resmap.put("media_id", fileinfo.getFileId());
	        resmap.put("type", type);
	        resmap.put("created_at", fileinfo.getCreateTime().getTime());
	        return BuildResult.buildOutResult(ResultEnum.SUCCESS, resmap);
	        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("保存文件信息失败",e);
		}
	}
  • springboot  文件下载接口
    @RequestMapping(value = "get",method = {RequestMethod.GET})
    @ResponseAnyBody
    public ResponseEntity<FileSystemResource> getFileById(@RequestParam String mediaId){
    	if(StringUtils.isBlank(mediaId)) {
    		return null;
    	}
    	ChatFileInfo fileInfo = this.wsFileService.getWsFileById(mediaId);
    	
    	if (fileInfo == null) {
    		LogUtil.info("文件不存在");
    		return null;
    	}
    	System.out.println(fileInfo.getFilePath());
    	File file = new File(fileInfo.getFilePath());
    	HttpHeaders headers = new HttpHeaders();
    	headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    	headers.add("Content-Disposition", "attachment; filename=" + file.getName());
    	headers.add("Pragma", "no-cache");
    	headers.add("Expires", "0");
    	headers.add("Last-Modified", new Date().toString());
    	headers.add("ETag", String.valueOf(System.currentTimeMillis()));
    	return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值