从上传的文件中获取文件扩展名
这里我的要求是上传文件并将其存储在磁盘中。 我将它存储在磁盘中没有问题,但获取文件的扩展名。 问题是当我单击上载并处理要存储在磁盘中的文件时,它将保存为具有以下名称的临时文件
“/ tmp目录/ multipartBody6238081076014199817asTemporaryFile”
这里的文件没有扩展名。 因此,以下任何库都无法帮助我获取文件的扩展名。
FileNameUtils.getExtension() or Files.getFileExtension(path)
我甚至试图通过它的属性来获取它,但它没有获取文件扩展名的选项。
Path path = Paths.get("/**/**/filepath"); BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
HTML代码:
从Play框架获取文件对象:
MultipartFormData body = request().body().asMultipartFormData(); FilePart fileInput = body.getFile("fileInput"); File file = fileInput.getFile();
任何提取文件扩展名的帮助都非常感谢。
谢谢,
我在客户端使用了Jquery fileupload 。
在我的JS文件中,
function doUploadPhoto(seq) { $('#fileupload').fileupload({ url : 'news/upload.html?s=' + seq, sequentialUploads : true, disableImageResize : false, imageMaxWidth : 1024, imageMaxHeight : 1024, previewCrop : true, dropZone : $("#dropZone"), acceptFileTypes : /(\.|\/)(gif|jpe?g|png)$/i, progress : function(e, data) { if (data.context) { var progress = data.loaded / data.total * 100; progress = Math.floor(progress); $('.progress').attr('aria-valuenow', progress); $('.progress').css('display', 'block'); $('.bar').css('width', progress + '%'); } }, progressall : function(e, data) { var progress = data.loaded / data.total * 100; progress = Math.floor(progress); $('.progressall').attr('aria-valuenow', progress); $('.progressall').css('display', 'block'); $('.allbar').css('width', progress + '%'); if (progress > 20) { $('.allbar').text(progress + '% Completed'); } }, stop: function (e) { return; } }); }
处理您对图像上传的具体要求(我使用过Spring)。
@RequestMapping(value = "/news/upload.html", method = RequestMethod.POST) public final void uploadNewsPhoto(final HttpServletRequest request, final HttpServletResponse response) throws Exception { doUploadNewsPhoto(request, getSessionFileItems(request)); }
用于上传图片
public final synchronized String doUploadNewsPhoto(final HttpServletRequest request, final List sessionFiles) throws UploadActionException { try { List
用于保存图像
public final Map saveToFile(final Long newsSeq, final FileItem item, final Long loginUserSeq) throws BusinessException { String staticDir = System.getProperty("staticDir"); Date today = new Date(); SimpleDateFormat fmtYMD = new SimpleDateFormat("/yyyyMMdd"); SimpleDateFormat fmtHMS = new SimpleDateFormat("HHmmssS"); String saveDir = "data/news" + fmtYMD.format(today); String format = ".jpg"; try { format = item.getName().substring(item.getName().lastIndexOf("."), item.getName().length()); } catch (Exception e) { format = ".jpg"; } try { String fileName = newsSeq + "_" + fmtHMS.format(today) + format; NewsPhotoBean bean = new NewsPhotoBean(); bean.setNewsSeq(newsSeq); bean.setFile(saveDir + "/" + fileName); // save image infos in database and return it's sequence Long photoSeq = newsPhotoService.add(bean, loginUserSeq); // Save image in specify location String filePath = staticDir + "/" + saveDir; FileSupport.saveFile(filePath, fileName, item); Map ret = new HashMap(); ret.put("seq", newsSeq); ret.put("photoSeq", photoSeq); ret.put("path", saveDir + "/" + fileName); ret.put("ext", format.substring(1)); //client side may need uploaded images info return ret; } catch (Exception e) { throw new BusinessException("Error occur when save file. newsSeq : " + newsSeq, e); } }
用于写入图像
// Save Image by FileItem that gets from Image Upload public static String saveFile(final String filePath, final String fileName, final FileItem item) throws Exception { File file = new File(filePath); file.setExecutable(true, false); file.setWritable(true, false); if (!file.exists()) { file.mkdirs(); } File imageFile = new File(file, fileName); item.write(imageFile); item.setFieldName(filePath + fileName); return item.toString(); }
我找到了解决方案。 实际上这是Play框架。 我使用以下代码获取了该文件。
MultipartFormData body = request().body().asMultipartFormData(); FilePart fileInput = body.getFile("fileInput"); File file = fileInput.getFile();
我试图使用这个File对象(用于存储在tmp位置)获取文件名。 但我没注意到FilePart对象包含上传的所有文件详细信息。 然后我想通了。
fileInput.getFilename()
为我提供了带扩展名的上传文件名。 它解决了我的问题。
感谢Cataclysm帮助我。 当然,他给出的是对Struts / Spring或核心servlet等其他框架的最佳答案。
这对我有用
Part filePart = request.getPart("input-file"); String type=filePart.getContentType(); type="."+type.substring(type.lastIndexOf("/")+1);