使用CommonsMultipartResolver上传文件时是否可以识别文件格式(Spring 3)

时间:2011-07-05 05:31:20

标签: java spring file-upload

我正在尝试使用Spring的CommonsMultipartResolver作为CommonsMultipartFile从浏览器上传PDF文件。是否可以验证上传的文件仅为PDF格式?

1 个答案:

答案 0 :(得分:0)


MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Iterator it = (Iterator) multipartRequest.getFileNames();
String fileName = it.next();  
MultipartFile file = multipartRequest.getFile(fileName);
String ext = fileName.substring(fileName.lastIndexOf("."));

if(ext.equalsIgnoreCase(".pdf")) {

    //do  something
}
else
{

        //do  something
}