HTML5+Spring-MVC实现手机端上传图片
来源:互联网 发布:网站域名注册流程 编辑:程序博客网 时间:2024/04/29 20:47
项目是挂在微信端的,所以手机图片上传的功能的比较难的就是怎样打开手机的相册,然后读取照片。最后在Controller中读取文件,然后copy到指定的路径下。
小白在网上找了一些资料然后结合了大神给的例子,简单的实现了手机端上传图片的功能。
以下就是具体的代码:
2.在Controller中的代码
3.在spring-mvc的配置文件中配置信息:
总结:这种手机上传图片的实现方法比较简单,配置信息和前台的图片获取的时候的代码信息较少,适合新手操作。另外,正是因为比较简单,所以这中方法上传图片功能比较简单,而且在部分手机上操作的时候总是提示手机只能上传内存卡上的照片信息(安卓),如果手机没有内存卡,会提示无法上传照片。具体原因还在努力寻找……
小白在网上找了一些资料然后结合了大神给的例子,简单的实现了手机端上传图片的功能。
以下就是具体的代码:
1.在jsp页面中的代码:
<span style="font-size:24px;"><form method="post" action="<%=rootPath%>/complaint" onSubmit="return emptyCheck()" enctype="multipart/form-data"><div id="container4-2" class="font20" style="background-color:#9fa0a0;margin-top:px;padding-top:0px;text-align:center;border:0px;border-bottom-left-radius:3px;border-bottom-right-radius: 3px;color:#ffffff;"><div style="margin-top:2px;"></div><!-- <input type="file" value="上传图片" style="border-width: "/> --><input type="file" name="file" style="position: absolute;opacity: 0.00"/></div></form></span>
2.在Controller中的代码
<span style="font-size:24px;"> </span><pre name="code" class="java"><span style="font-size:24px;"> //【解析器解析request的上下文】 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //先判断request中是否包涵multipart类型的数据, if(multipartResolver.isMultipart(request)){ //再将request中的数据转化成multipart类型的数据 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator iter = multiRequest.getFileNames(); while(iter.hasNext()){ MultipartFile file = multiRequest.getFile((String)iter.next()); if(file != null){ fileName = file.getOriginalFilename(); String typeString=fileName.substring(fileName.lastIndexOf(".")); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String id = sdf.format(new Date()); nameString=id+typeString; path = "C:\\newtomcat\\webapps\\mobile\\userImg"; localFile = new File(path , nameString); clientComplaint.setPath(localFile.getPath());// //写文件到本地 try { FileUtils.copyInputStreamToFile(file.getInputStream(),new File(path,nameString)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } </span>
3.在spring-mvc的配置文件中配置信息:
<span style="font-size:24px;"><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="209715200" /><property name="defaultEncoding" value="UTF-8" /><property name="resolveLazily" value="true" /><!-- 延迟加载 --></bean></span>
总结:这种手机上传图片的实现方法比较简单,配置信息和前台的图片获取的时候的代码信息较少,适合新手操作。另外,正是因为比较简单,所以这中方法上传图片功能比较简单,而且在部分手机上操作的时候总是提示手机只能上传内存卡上的照片信息(安卓),如果手机没有内存卡,会提示无法上传照片。具体原因还在努力寻找……
0 0
- HTML5+Spring-MVC实现手机端上传图片
- 使用spring mvc+localResizeIMG实现HTML5端图片压缩上传
- spring mvc实现图片上传
- Spring mvc实现文件上传及html5实现上传进度条
- Spring MVC 实现图片上传功能
- CKEditor图片上传实现(spring mvc)
- spring mvc+ajaxfileupload 实现异步上传图片
- Java - spring mvc 上传图片模块实现
- spring mvc 图片上传
- spring mvc 上传图片
- Spring MVC 图片上传
- Spring mvc图片上传
- spring mvc 上传图片
- spring mvc 图片上传
- spring mvc 图片上传
- spring mvc 上传图片
- Spring MVC-图片上传
- Spring MVC上传图片
- leetcode 93. Restore IP Addresses
- protect your eye
- 安卓-android.intent.category.DEFAULT
- Hibernate 相关jar包详细介绍 .
- Android 程序员必备
- HTML5+Spring-MVC实现手机端上传图片
- magento 笔记
- [MS Office使用] Word无法合并单元格
- centos命令
- 安装orcale11g 出现[INS-07009] 无法加载 BeanStore
- 监控脚本程序
- magento 后台menu 添加item
- web项目报404一直找不到图片(路径绝对正确)
- RabbitMQ (五)主题(Topic)