如果您知道为什么它不起作用,请提供帮助。几个小时以来,我一直在尝试使用 Postman 对其进行测试,但没有成功。
导入控制器:
package demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.servlet.annotation.MultipartConfig;
@RestController
@RequestMapping("/imports")
@MultipartConfig(fileSizeThreshold = 20971520) //20MB
public class ImportController {
@Autowired
private ImportRepository importRepository;
@PostConstruct
public void init() {
System.out.println("ImportController initialized.");
}
// Create a new import
@PostMapping
@RequestMapping(value = "/", method = RequestMethod.POST, consumes = "multipart/form-data")
public ImportModel createImport(@RequestParam("file") MultipartFile file) throws IOException {
// Save the file to the server's file system
Path filePath = Paths.get("", file.getOriginalFilename());
file.transferTo(filePath);
// Create an ImportModel object with the file path and save it to the database
ImportModel importModel = new ImportModel();
importModel.setFilename(file.getOriginalFilename());
importModel.setFilePath(filePath.toString());
return importRepository.save(importModel);
}
这是有请求并应该有映射的控制器
您的代码需要“/imports/”。
上课:
@RequestMapping("/imports")
然后再谈方法:
@PostMapping
@RequestMapping(value = "/", . . . .
忘记第二个
@RequestMapping
,只用
@PostMapping(consumes = "multipart/form-data")
真的没必要混在一起做双重定义