通过 Java REST Api 中的 POST 请求传输 .xls 文件
Transfer .xls files through POST request in Java REST Api
我是 Spring 的新手,我在通过 POST 请求从前端接收 .xls 文件时遇到了一些麻烦。基本上,当我访问 link "Project/File" 时,我收到错误代码 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
}
}
谁能告诉我如何解决这个问题?
您似乎正在尝试使用 Jax-RS @Consumes
注释实现文件上传控件。我看到的第一个问题是您试图直接使用 application/vnd.ms-excel
,但这不是您的文件上传从前端呈现的内容。它期望 multipart/form-data
作为 mime 类型。这样做,然后您可以在请求映射中使用 @RequestParam("file") MultipartFile file
参数来获取文件。
我从未在我的应用程序中使用过 Jax-RS 注释,但您甚至不需要它。您的请求映射可以像这样简单:
@RequestMapping(value ="Project/File", method = RequestMethod.POST)
public void handleFileUpload(@RequestParam("file") MultipartFile file)
Spring 有一个很好的指南,显示了如果您想查看更多详细信息,您需要为文件上传做什么 https://spring.io/guides/gs/uploading-files/
我还要注意,你已经用 @RequestBody
注释了映射的 return 类型,但是你的 return 类型是 void
,所以没有必要对于 @RequestBody
在这种情况下。我建议至少有一个 return 类型的 HttpStatus
然后 returning HttpStatus.OK
如果上传成功。
我是 Spring 的新手,我在通过 POST 请求从前端接收 .xls 文件时遇到了一些麻烦。基本上,当我访问 link "Project/File" 时,我收到错误代码 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
}
}
谁能告诉我如何解决这个问题?
您似乎正在尝试使用 Jax-RS @Consumes
注释实现文件上传控件。我看到的第一个问题是您试图直接使用 application/vnd.ms-excel
,但这不是您的文件上传从前端呈现的内容。它期望 multipart/form-data
作为 mime 类型。这样做,然后您可以在请求映射中使用 @RequestParam("file") MultipartFile file
参数来获取文件。
我从未在我的应用程序中使用过 Jax-RS 注释,但您甚至不需要它。您的请求映射可以像这样简单:
@RequestMapping(value ="Project/File", method = RequestMethod.POST)
public void handleFileUpload(@RequestParam("file") MultipartFile file)
Spring 有一个很好的指南,显示了如果您想查看更多详细信息,您需要为文件上传做什么 https://spring.io/guides/gs/uploading-files/
我还要注意,你已经用 @RequestBody
注释了映射的 return 类型,但是你的 return 类型是 void
,所以没有必要对于 @RequestBody
在这种情况下。我建议至少有一个 return 类型的 HttpStatus
然后 returning HttpStatus.OK
如果上传成功。