作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在开发一个具有文件上传功能的 Spring-MVC 应用程序。为此,我想确定用户上传的文件是否是一种图像。
我想创建该图像的缩略图预览,这就是为什么我需要确定它是否是图像的原因。我有缩略图创建代码,只是不知道它是否是图像。
请求代码:
@RequestMapping(value = "/notes/addattachment/{noteid}/{groupaccountid}/{api}", method = RequestMethod.POST)
public @ResponseBody void addAttachments(@PathVariable("noteid") int noteid, @PathVariable("groupaccountid") Long groupAccountId,
@PathVariable("api") String api, @RequestParam("attachments") MultipartFile attachment) {
if (!(attachment.isEmpty())) {
switch (api){
case "somecase":
try {
String fileName = attachment.getOriginalFilename();
long fileSize = attachment.getSize();
byte[] bytes = attachment.getBytes();
this.groupAttachmentsService.addAttachment(bytes, fileName, fileSize, noteid, true,attachment.getContentType());
} catch (Exception ignored) {}
break;
case "google":
this.driveQuickstart.insertFile(attachment,noteid, groupAccountId,"123");
break;
case "dropbox":
String path = api.replace(":","/");
this.dropboxTask.insertFile(attachment,"path",noteid, groupAccountId);
break;
}
}
}
任何帮助都会很好。谢谢。
最佳答案
如果您使用的是 java 7 ImageIO
是您可以用来检查文件是否为图像的类。 ImageIO read()如果找不到图像,此方法返回 null。
关于Java,多部分 : Determine if multipart uploaded is a type of image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32689899/
我是一名优秀的程序员,十分优秀!