1

我正在尝试使用表单上传图片,它将成为个人资料图片,但出现错误:

所需的请求部分“文件”不存在

我对spring真的很陌生,所以我不知道这里有什么问题是代码的一部分:

这是表格

<form th:action="@{/form}" th:object="${cliente}" method="post" enctype="multipart/form-data">

    <div class="form-group">
        <label>Nombre</label>
        <input class="form-control" type="text" th:field="*{nombre}" placeholder="Nombre"/>
        <small class="form-text text-danger" th:if="${#fields.hasErrors('nombre')}" th:errors="*{nombre}"></small>
    </div>

    <div class="form-group">    
        <label>Apellido</label>
                    <input class="form-control" type="text" th:field="*{apellido}" placeholder="Apellido"/>
        <small class="form-text text-danger" th:if="${#fields.hasErrors('apellido')}" th:errors="*{apellido}"></small>
    </div>
    <div class="form-group">    
        <label>Email</label>
        <input class="form-control" type="text" th:field="*{email}" placeholder="correo@ejemplo.com"/>
        <small class="form-text text-danger" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></small>
    </div>
    <div class="form-group">    
        <label>Fecha</label>
        <input class="form-control" type="text" th:field="*{createAt}" placeholder="DD/MM/YYYY"/>
        <small class="form-text text-danger" th:if="${#fields.hasErrors('createAt')}" th:errors="*{createAt}"></small>
    </div>
    <div class="form-group">
        <label>Imagen</label>
        <input type="file" name="file" class="form-control" th:field="*{foto}">
    </div>
    <div class="form-group">    
        <input class="btn btn-primary" type="submit" value="Guardar Cambios" />
    </div>

 <input type="hidden" th:field="*{id}" />
</form>

这是控制器功能

@RequestMapping(value="/form", method=RequestMethod.POST)
    public String guardar(@Valid Cliente cliente, BindingResult result, 
@RequestParam("file") MultipartFile foto, RedirectAttributes flash) {

    String mensaje;

    if(result.hasErrors()) {
        return "form";
    }

    if(!foto.isEmpty()) {
        Path directorioRecursos = Paths.get("src//main//resources//static/uploads");
        String pathRoot = directorioRecursos.toFile().getAbsolutePath();
        try {
            byte[] bytes = foto.getBytes();

            Path rutaCompleta = Paths.get(pathRoot + "//" + foto.getOriginalFilename());

            Files.write(rutaCompleta, bytes);

            flash.addFlashAttribute("info", "Foto subida correctamente");

            cliente.setFoto(foto.getOriginalFilename());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    if(cliente.getId() != null) {
        mensaje = "Cliente actualizado correctamente";
    }
    else {
        mensaje = "Cliente creado correctamente";
    }

    clienteservice.save(cliente);
    flash.addFlashAttribute("success", mensaje);
    return "redirect:listar";           
}

这是我的应用程序属性

spring.http.multipart.file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.enabled=true

你能看出有什么问题吗?

4

1 回答 1

1

您正在使用 th:field="*{foto}" 将文件附加到字符串字段,

删除输入中的 th:field 部分,应该是这样的:

                        <input type="file" name="file" class="form-control" /> 
于 2019-06-12T20:06:29.450 回答