zoukankan      html  css  js  c++  java
  • 根据上传的MultipartFile通过springboot转化为File类型并调用通过File文件流的方法上传特定服务器

     
     1     @PostMapping("uploadExcel")
     2      public ResponseObj uploadExcel(@RequestParam("excelFile") MultipartFile file,@RequestParam("companyId") String companyId,
     3              @RequestParam("productId") String productId,HttpServletRequest request) throws Exception {
     4         ResponseObj response = new ResponseObj();
     5         response.setData(Defined.STATUS_SUCCESS);
     6         response.setMessage("文件上传成功!");
     7         ResponseObj resp = new ResponseObj();
     8         resp.setData(Defined.STATUS_ERROR);
     9         resp.setMessage("不是文件!");
    10         AliYunFileSetting setting = new AliYunFileSetting();
    11         setting.setAccessKeyId(aliConstants.accessKeyId);
    12         setting.setAccessKeySecret(aliConstants.accessKeySecret);
    13         setting.setBucketName(aliConstants.bucketName);
    14         setting.setEndpoint(aliConstants.endpoint);
    15         AliyunFileManager manager = AliyunFileManager.getInstance(setting);
    16         if(file.isEmpty()){
    17             response.setData(Defined.STATUS_ERROR);
    18             response.setMessage("不是文件!");
    19             return response;
    20            }
    21         String fileName = file.getOriginalFilename();
    22         long fileSize = file.getSize();
    23         File path = new File(ResourceUtils.getURL("classpath:").getPath());
    24         if(!path.exists()) path = new File("");
    25         File upload = new File(path.getAbsolutePath(),"static/images/upload/");
    26         if(!upload.exists()) upload.mkdirs();
    27         File tempFile = new File(upload+"/"+fileName);
    28         if(!tempFile.getParentFile().exists()){
    29             tempFile.getParentFile().mkdirs();//创建父级文件路径
    30             tempFile.createNewFile();//创建文件
    31         }
    32         String relativePath =aliConstants.excelFilePath;//相对路径
    33         String dir =  aliConstants.aliyunHostOuter+"/"+relativePath;//云端绝对路径
    34         File dest = new File(dir);
    35         if(!dest.exists()){ //判断文件目录是否存在
    36             dest.mkdir();//
    37         }
    38         try {
    39             file.transferTo(tempFile); 
    40             InputStream is = new FileInputStream(tempFile);
    41             String suffix=fileName.substring(fileName.lastIndexOf("."));//获取原始文件后缀.xlxs(含点)
    42             String newFileName = IdGen.uuid()+suffix;
    43             boolean result = manager.upload(is, relativePath, newFileName);//上传文件,并建立随机文件名。
    44 //          boolean result = manager.upload(is, dir, fileName);//上传阿里云,固定文件名
    45             if(result){
    46                  response.setData(dir+"/"+newFileName);//返回新建文件名的绝对路径
    47 //                 数据库存入相对路径
    48                  BatchRecord batchRecord = new BatchRecord();
    49                  batchRecord.setFileName(newFileName);
    50                  batchRecord.setFilePath(relativePath+"/"+newFileName);
    51                  batchRecord.setFileSize(fileSize);
    52                  batchRecord.setIsDelete((byte) 0);
    53                  batchRecord.setStatus((byte) 0);
    54                  batchRecord.setId(IdGen.uuid());
    55                  batchRecord.setCreateTime(DateUtil.getNowTimestamp());
    56                  batchRecord.setUpdateTime(DateUtil.getNowTimestamp());
    57                  batchRecord.setCompanyId(companyId);
    58                  batchRecord.setProductId(productId);
    59                  Integer resultNum  = deviceService.addBatchRecord(batchRecord);
    60                  if(resultNum>0){
    61                  return response;
    62                  }
    63                  return resp;
    64             }else{
    65                 resp.setMessage("文件上传失败!");
    66                 return resp;
    67             }
    68         } catch (IllegalStateException e) {
    69             // TODO Auto-generated catch block
    70             e.printStackTrace();
    71             resp.setMessage("文件上传异常!");
    72             return resp;
    73         } catch (IOException e) {
    74             // TODO Auto-generated catch block
    75             e.printStackTrace();
    76             resp.setMessage("文件上传异常!");
    77             return resp;
    78         }
    79     }

    参考的博客https://blog.csdn.net/heylun/article/details/78732451

    内容

    springboot部署之后无法获取项目目录的问题:

    之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行时,项目中文件上传的功能无法正常运行,其中获取到存放文件的目录的绝对路径的值为空,文件无法上传。问题链接

    不清楚此网友具体是怎么实现的,通常我们可以通过如下方案解决:

    //获取跟目录
    File path = new File(ResourceUtils.getURL("classpath:").getPath());
    if(!path.exists()) path = new File("");
    System.out.println("path:"+path.getAbsolutePath());
    
    //如果上传目录为/static/images/upload/,则可以如下获取:
    File upload = new File(path.getAbsolutePath(),"static/images/upload/");
    if(!upload.exists()) upload.mkdirs();
    System.out.println("upload url:"+upload.getAbsolutePath());
    //在开发测试模式时,得到的地址为:{项目跟目录}/target/static/images/upload/
    //在打包成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/

    另外使用以上代码需要注意,因为以jar包发布时,我们存储的路径是与jar包同级的static目录,因此我们需要在jar包目录的application.properties配置文件中设置静态资源路径,如下所示:

    #设置静态资源路径,多个以逗号分隔
    spring.resources.static-locations=classpath:static/,file:static/

    以jar包发布springboot项目时,默认会先使用jar包跟目录下的application.properties来作为项目配置文件。

  • 相关阅读:
    项目纪实一
    Quartz.net一个简要示例
    ASP.NET MVC4 WebAPI若干要点
    利用委托实现父控件与子控件之间消息传递
    js获取屏幕信息
    jsion大括号和中括号,及调用
    jquery解析jsion
    hibernate初步
    java web笔记
    mysql存储过程
  • 原文地址:https://www.cnblogs.com/xiaoyao-001/p/9308744.html
Copyright © 2011-2022 走看看