我是 Spring 新手,在通过 POST 请求从前端接收 .xls 文件时遇到了一些麻烦。基本上,当我访问“项目/文件”链接时,我收到错误代码 400(错误请求),但前端和后端的参数名称相同。
到目前为止,这是我的代码:
@Controller
public class RestController {
@Autowired
private FileService fileService;
@Consumes("application/vnd.ms-excel")
@RequestMapping(value = "Project/File",
method = {RequestMethod.POST, RequestMethod.GET},
params = {"inputXLS"})
public @ResponseBody void getCitiriContoriMecanici(
@RequestParam(value = "inputXLS" , required = true) InputStream inputStream) throws IOException{
//transform the InputStream to an array of bytes;
byte[] bytes = IOUtils.toByteArray(inputStream);
//create a new .xls file
File file = new File("TEST.xls");
FileOutputStream fileOutputStream = new FileOutputStream(file);
//write a the bytes to a new .xls file
fileOutputStream.write(bytes);
fileOutputStream.flush();
fileOutputStream.close();
fileService.getFileFromFrontEnd();//this method is used to parse the .xls file
}
}
谁能给我一个想法如何解决这个问题?