对于某些缓存文件信息敏感,需反复擦除
@PostMapping("upload")
public Object up(HttpServletRequest request){
Object obj = null;
File file = null;
try {
MultipartHttpServletRequest request1 = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = request1.getFile("file");
ApplicationPart part = (ApplicationPart)request1.getPart("file");
Class<?> Clazz = part.getClass();
Field field;
if ((field = getField(Clazz, "location")) != null){
field.setAccessible(true);
try {
obj = field.get(part);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
file = (File) obj;
if ((field = getField(Clazz, "fileItem")) != null){
field.setAccessible(true);
try {
obj = field.get(part);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
file = (File) obj;
DiskFileItem fileItem =(DiskFileItem)obj;
log.info(fileItem.getStoreLocation().toString());
log.info(obj.toString());
log.info(file.getAbsolutePath());
log.info(file.getName());
log.info(file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
return file;
} catch (ServletException e) {
e.printStackTrace();
return file;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return file;
}
该段代码展示了如何在Spring MVC环境中处理上传的文件。它首先将`HttpServletRequest`转换为`MultipartHttpServletRequest`,然后尝试从`ApplicationPart`获取文件的`location`和`fileItem`字段。获取到的文件信息被打印出来,包括存储位置、绝对路径、文件名和规范化的路径。如果在处理过程中出现IOException、ServletException或IllegalAccessException,文件将被返回。
581

被折叠的 条评论
为什么被折叠?



