SpringBoot 文件上传操作

文件上传

  1. 利用org.springframework.web.multipart.MultipartFile类(该方法存在一定不足)
/** 
 * 该方法属于上传保存后,得到保存后的路径
 * @param file
 * @return
 */
public String videoUpload(MultipartFile file) {
    //该方法使用MultipartFile 
    //COMMON_EMPTY 为空
    String realPath = MyConstants.COMMON_EMPTY;
    String saveUrl = MyConstants.COMMON_EMPTY;
    try {
        String path = request.getSession().getServletContext().getRealPath("/") + MyConstants.UPLOAD_VIDEO;
        File dir = new File(path);
        //检查路径是否存在
        if (!dir.exists()) {
            //设置路径
            dir.mkdir();
        }
        String date = String.valueOf(System.currentTimeMillis());
        String filename = file.getOriginalFilename();
        String prefix=filename.substring(filename.lastIndexOf(".")+1);
        //生成唯一的UUID编号
        String data = UUID.randomUUID().toString();
        //该realPath是要上传传保存的路径,
        realPath = path + date + data + MyConstants.VIDEO_DIRECTORY + "." + prefix;
        File f = new File(realPath);
        file.transferTo(f);
        //获得工程的相对路径
           saveUrl = MyConstants.UPLOAD_VIDEO+ date + data + MyConstants.VIDEO_DIRECTORY + "." + prefix;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return saveUrl;
}
  1. 利用org.springframework.web.multipart.MultipartHttpServletRequest类
/**
 * 视频上传
 * @param file
 * @return
 */
public String videoUpload(MultipartHttpServletRequest multiReq) {
    String realPath = MyConstants.COMMON_EMPTY;
    String saveUrl = MyConstants.COMMON_EMPTY;
    String filename = multiReq.getFile("file").getOriginalFilename();
    // 截取上传文件的后缀
    String prefix=filename.substring(filename.lastIndexOf(".")+1);
    FileOutputStream fos = null;
    FileInputStream fis = null;
    try {
      String path = request.getSession().getServletContext().getRealPath("/") + MyConstants.UPLOAD_VIDEO;
      fis = (FileInputStream) multiReq.getFile("file").getInputStream();
      String date = String.valueOf(System.currentTimeMillis());
      String data = UUID.randomUUID().toString();
      realPath = path + date + data + MyConstants.VIDEO_DIRECTORY + "." + prefix;
      fos = new FileOutputStream(realPath);
      //设置文件缓冲区
      byte[] temp = new byte[1024];
      int i = fis.read(temp);
      while (i != -1){
        fos.write(temp,0,temp.length);
        fos.flush();
        i = fis.read(temp);
      }
    //获得工程的相对路径
       saveUrl = MyConstants.UPLOAD_VIDEO+ date + data + MyConstants.VIDEO_DIRECTORY + "." + prefix;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return saveUrl;
}

然后利用返回的路径,拼接相对路径和绝对路径,为文件的显示和删除做准备

/**
 * 上传视频
 * @param file
 */
@RequestMapping(value = "/uploadcourseVideo", method = RequestMethod.POST)
public @ResponseBody void uploadVideo(MultipartHttpServletRequest multiReq) {
//uploadVideo方法的参数可以调整为MultipartFile file
    String port = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
    String realpath = this.videoUpload(multiReq);
    String path = request.getSession().getServletContext().getRealPath("/");
    //生成文件的相对路径
    String relativePath = port + realpath;
    //生成文件的绝对路径
    String absolutePath = path + realpath;
    FileUploadRotation fileUploadRotation = new FileUploadRotation();
    if("".equals(realpath)) {
        fileUploadRotation.setCode("0");
        fileUploadRotation.setMsg("视频上传失败");
    } else {
        //将路径传递给前台
        fileUploadRotation.setAbsolutePath(absolutePath);
        fileUploadRotation.setRelativePath(relativePath);
        fileUploadRotation.setCode("1");
    }
    outPrintFileUpload(fileUploadRotation);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值