简体   繁体   中英

Spring boot REST api file upload

I wrote some code to create a REST api to handle customer's infomation and a file from them,I want to save the file they upload to my local disk on server.My code is as below

 @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();
    }
}

Problem is a java.lang.NoClassDefFoundError:org/apache/commons/fileupload/FileUploadException] with root cause.So how can I do this?Thanks in advance.

Have you tried this one on pom.xml or add commons-fileupload.jar :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM