利用原生的 file input上传 图片
2017-08-03 15:53
381 查看
public void addPic(@RequestParam(value = "pic", required = false)MultipartFile pic) throws IOException { if (pic != null) { if (FileUtil.isAllowUpImg(pic.getOriginalFilename())) { InputStream stream=null; try { stream=pic.getInputStream(); } catch (Exception e) { e.printStackTrace(); } String subFolder = "adv";//路径中添加的一个字符串 String fileName=pic.getOriginalFilename(); if(stream==null){ throw new IllegalArgumentException("file or filename object is null"); } /** * 判断图片是否允许的类型 */ if(!FileUtil.isAllowUpImg(fileName)){ throw new IllegalArgumentException("不被允许的上传文件类型"); } /* 生成文件的路径 */ String ext = FileUtil.getFileExt(fileName); fileName = DateUtil.toString(new Date(), "mmss") + StringUtil.getRandStr(4) + "." + ext; String static_server_path= SystemSetting.getStatic_server_path(); String filePath = static_server_path + "/attachment/"; if(subFolder!=null){ filePath+=subFolder +"/"; } Calendar now = Calendar.getInstance(); int year=now.get(Calendar.YEAR); int month=now.get(Calendar.MONTH) + 1; int date=now.get(Calendar.DAY_OF_MONTH); int minute=now.get(Calendar.HOUR_OF_DAY); String timePath=""; if(year!=0){ timePath+=year+"/";; } if(month!=0){ timePath+=month+"/";; } if(date!=0){ timePath+=date+"/";; } if(minute!=0){ timePath+=minute+"/";; } String path = EopSetting.FILE_STORE_PREFIX+ "/attachment/" +(subFolder==null?"":subFolder)+"/"+timePath+"/"+fileName; filePath +=timePath; filePath += fileName; /** * 写入文件 将文件写出 */ FileUtils.copyInputStreamToFile(stream, new File(filePath)); } else { System.out.println("不允许上传的文件格式,请上传gif,jpg,bmp,swf格式文件。"); } } }
/** * 是否是允许上传的图片格式 * @param imgFileName * @return */ public static boolean isAllowUpImg(String imgFileName){ imgFileName = imgFileName.toLowerCase(); String allowTYpe = "gif,jpg,bmp,png,jpeg,swf,mp4"; if (!imgFileName.trim().equals("") && imgFileName.length() > 0) { String ex = imgFileName.substring(imgFileName.lastIndexOf(".") + 1, imgFileName.length()); return allowTYpe.toUpperCase().indexOf(ex.toUpperCase()) >= 0; } else { return false; } }form表单就很简单
<form action="/core/admin/adv/add-savetest.do" method="post" enctype="multipart/form-data"> <input type="file" label="文件上传" name="pic" accept="image/*,.swf"> <button type="submit">提交</button> </form>
在测试过程中遇到一个问题
org.springframework.web.multipart.MultipartException: The current request is not a multipart request
当时就是应为没有写 enctype='multipart/form-data'
参考链接
相关文章推荐
- 简易上传图片原生input-file实现方案
- AngularJS用原生的fileinput上传图片
- 利用SpringMVC原生封装的对象实现图片上传及图片回显功能的实现及案例代码
- Nginx+Ftp+Fileinput做图片上传显示服务器(三)
- input type="file" 上传文件,若是flv格式,则截取保存一张图片asp.net
- html input控件type为file上传文件、图片,及保存为原文件
- input type=file实现图片上传,预览以及图片删除
- yii2利用自带UploadedFile上传图片
- input标签 type='file' 上传图片问题
- input[type=file]中使用ajaxSubmit来图片上传
- 最近项目上传图片遇到input="file" 不能清空在ie10及以下
- bootstrap 上传图片插件 file-input 的简单使用
- 添加图片如何点击a标签, 弹出input file 上传文件对话框
- 利用html5的FileReader对象实现图片预览,利用FormData对象结合struts2实现无刷新文件上传(多参数)
- Bootstrap fileinput的图片上传 java框架基于springMVC
- 利用zend framework的Zend_File_Transfer_Adapter_Http类实现多个图片的上传
- input-file 图片上传尺寸验证
- input标签file文件上传图片,获取base64
- html,图片上传预览,input file获取文件等相关操作
- 将input file 上传组件替换成一张图片的简易方式