[英]Upload file with spring boot
I'm trying to do a simple file upload, but it's giving an error, below is how I did it.我正在尝试进行简单的文件上传,但它给出了一个错误,下面是我是如何做到的。
Form:形式:
<div class="container">
<div th:replace="~{base :: titulo('Disparar Holerites')}"></div>
<div class="card mt-3">
<form class="card-body needs-validation" action="/holerite/uploadHoleritesFile" method="POST" enctype="multipart/form-data" novalidate>
<div>
<label for="arquivoContabilidade" class="form-label">Arquivo Recebido da Contabilidade</label>
<div class="input-group has-validation mb-4">
<input accept=".pdf" type="file" class="form-control" id="arquivoContabilidade" aria-describedby="inputGroupPrepend" required>
</div>
</div>
<button class="btn btn-primary" type="submit">Enviar</button>
</form>
</div>
Controller: Controller:
@PostMapping(value = "uploadHoleritesFile")
public String uploadHoleritesFile(@RequestParam("arquivoContabilidade") MultipartFile arquivoContabilidade, RedirectAttributes redirectAttributes){
logger.info("Entramos aqui");
logger.info("Recebido o arquivo: "+!arquivoContabilidade.isEmpty());
if(arquivoContabilidade.isEmpty()){
return "redirect:holerites/passo1";
}
try{
byte[] bytes = arquivoContabilidade.getBytes();
Path path = Paths.get(COTABILIDADE_FOLDER + arquivoContabilidade.getOriginalFilename());
Files.write(path, bytes);
} catch (IOException e){
e.printStackTrace();
}
return "holerites/passo2";
}
Error:错误:
2022-02-04 18:36:41.048 DEBUG 17136 --- [nio-8080-exec-9] osweb.servlet.DispatcherServlet: "ERROR" dispatch for POST "/error", parameters={multipart} 2022-02-04 18:36:41.057 DEBUG 17136 --- [nio-8080-exec-9] swsmmaRequestMappingHandlerMapping: Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse) 2022-02-04 18:36:41.077 DEBUG 17136 --- [nio-8080-exec-9] oswsvContentNegotiatingViewResolver: Selected 'text/html' given [text/html, text/html;q=0.8] 2022-02-04 18:36:41.081 DEBUG 17136 --- [nio-8080-exec-9] osweb.servlet.DispatcherServlet: Exiting from "ERROR" dispatch, status 403 2022-02-04 18:36:41.048 DEBUG 17136 --- [nio-8080-exec-9] osweb.servlet.DispatcherServlet:POST“/error”的“ERROR”调度,参数={multipart} 2022-02- 04 18:36:41.057 调试 17136 --- [nio-8080-exec-9] swsmmaRequestMappingHandlerMapping: 映射到 org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(202-0, 2-HttpServletResponse) 04 18:36:41.077 调试 17136 --- [nio-8080-exec-9] oswsvContentNegotiatingViewResolver: 给定 [text/html, text/html;q=0.8] 2022-02-04 18:36 选择的 'text/html' :41.081 DEBUG 17136 --- [nio-8080-exec-9] osweb.servlet.DispatcherServlet:退出“错误”调度,状态 403
You should add an attribute name="arquivoContabilidade"
to your input field in your html.您应该在 html 的输入字段中添加一个属性
name="arquivoContabilidade"
。 Thymeleaf uses the name
attribute when it maps it to a multipart file. Thymeleaf 在将其映射到多部分文件时使用
name
属性。
Example:例子:
<input accept=".pdf" name="arquivoContabilidade" type="file" class="form-control" id="arquivoContabilidade" aria-describedby="inputGroupPrepend" required>
Also, I'm assuming that you controller has the annotation @RequestMapping("/holerite")
.另外,我假设您 controller 有注释
@RequestMapping("/holerite")
。
If it doesn't you will also have to change如果没有,你也必须改变
@PostMapping(value = "uploadHoleritesFile")
to至
@PostMapping(value = "/holerite/uploadHoleritesFile")
because your form has <form action="/holerite/uploadHoleritesFile" method="POST"...>
因为你的表单有
<form action="/holerite/uploadHoleritesFile" method="POST"...>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.