Spring启动REST api文件上传

时间:2017-10-23 13:07:02

标签: spring rest api spring-mvc spring-boot

我写了一些代码来创建一个REST api来处理客户的信息和来自他们的文件,我想将他们上传的文件保存到我服务器上的本地磁盘。我的代码如下

 @RequestMapping(value = "/alg", method = RequestMethod.POST)
public String postUsersNewAlg(@RequestParam(value = "file", required = true) CommonsMultipartFile jarfile,
                              @RequestParam(value = "username", required = true) String userName,
                              @RequestParam(value = "output", required = true) String output) {

   //other code handles string infos etc.

    try {
      //get directory path that to save file
      String filePath = PathUtil.getOffAlgJarFilePathRoot();

      //get file path 
      String filePathWhole = null;
      if (!filePath.endsWith(SeparatorUtils.getFileSeparator())) {
        filePathWhole = filePath + SeparatorUtils.getFileSeparator() + algImpl.getOriginalFilename();
         } 
      else {
        filePathWhole = filePath + algImpl.getOriginalFilename();
        }

    FileUtil.copyFile(jarfile, filePath,filePathWhole);
    }
    catch(Exception e){
        e.printStackTrace();
        return e.getMessage();
    }
}

问题是带有根本原因的java.lang.NoClassDefFoundError:org / apache / commons / fileupload / FileUploadException]。我怎么能这样做?提前感谢。

1 个答案:

答案 0 :(得分:1)

您是否曾在pom.xml上试过这个或添加commons-fileupload.jar

 <dependency>
   <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
   <version>1.2.1</version>
 </dependency>