如何使用Spring MVC获取所有文件名?

时间:2015-03-30 14:45:01

标签: java spring file api model-view-controller

我有一个按钮,它将文件名发送到API,但API只获取文件名(不包括扩展名)。我如何获得扩展?

JS

<button class="upl-btn" data-url="validacao/upload/planilha/55163343b0df070bbc66e1bb6e0c3f9b.xlsx" data-type="DELETE">
<span>Apagar</span>
</button> 

API

@RestController
@RequestMapping("/validacao/upload/planilha")
@Api(hidden = true, value = "/planilha", description = "API para envio de planilhas para validação")
public class UploadPlanilhaAPI {
    @RequestMapping(value = "/{name}",  method=RequestMethod.DELETE)
        public Response deleteFile(@PathVariable("name")String name){
            HttpSession session = activeUser.getSession();
            String path = (String) session.getAttribute("dir");
            uploadPlanilhaService.deletePlanilha(path, name);
            session.removeAttribute("json");
            session.removeAttribute("ignored");
            session.setAttribute("lastuploaddate", Util.now());
            return Response.ok().build();
        }
}

结果是name = 55163343b0df070bbc66e1bb6e0c3f9b,我想要name = 55163343b0df070bbc66e1bb6e0c3f9b.xlsx

1 个答案:

答案 0 :(得分:2)

您遇到了Spring的可选后缀功能。默认情况下,Spring允许您声明/myPage请求映射。现在,它允许您调用/myPage.jsp/myPage.json,Spring会将响应转换为所需类型。他们默认打开它是非常烦人的;特别是如果你的网址的最后一部分是一个路径变量:它最终会修剪最终的.部分,因为它认为你正在尝试使用这个功能。

如果您对此功能不感兴趣,可以将其关闭。

在servlet xml文件中,添加此path-matching设置:

<mvc:annotation-driven>
    <mvc:path-matching registered-suffixes-only="true"/>
</mvc:annotation-driven>

此处的替代解决方案: Spring MVC @PathVariable with dot (.) is getting truncated