SpringMVC+MyBaties关于上传(跟新)图片的问题
/**
* 方法名:uploadPhoto
* 描 述:TODO(上传图片)
* 作 者:池彦龙
* 时 间:2017/03/30
* 返回类型:
* 参 数:
* 异 常:
*/
Controller 层:
@RequestMapping(params = "method=uploadPhoto")// 注解RequestMapping
public void uploadPhoto(MultipartHttpServletRequest request,HttpServletResponse response){
Json json = new Json();
Parameter parameter;//获取页面传来值
Iterator<String> itr=request.getFileNames();
MultipartFile file = request.getFile(itr.next());
//获取项目的路径,保存到当前项目 webapps下面的target;注意此路径必须传,如果不传路径会保存到tomcat/bin路径下面(有的项目需要保存到指定盘时也不用传)
String baseUrl = request.getSession().getServletContext().getRealPath("/");
try{
parameter=initParameter(request);
json = indexService.uploadPhoto(file,baseUrl, parameter);
}catch (Exception e){
e.printStackTrace();
json.setCode("400");
json.setSuccess(false);
json.setMsg("上传失败");
}finally{
this.writeJson(request,response,json);
}
}
/**
* 方法名:uploadPhoto
* 描 述:TODO(上传图片)
* 作 者:池彦龙
* 时 间:2017/04/05
* 返回类型:
* 参 数:
* 异 常:
*/
Service 层:
public Json uploadPhoto(MultipartFile file,String baseUrl,Parameter parameter) throws IOException {
Json json=new Json();
Map<String,Object> requestMap=parameter.getRequestParamsMap();
// 上传到本地
String fileName = file.getOriginalFilename();
String filePath = ("app/photo/"+requestMap.get("userName")) ;
String[] fileNameArr=fileName.split("\\.");
//将文件名称改为UUID命名,防止重名
fileName=UUID.randomUUID()+"."+fileNameArr[1];
File savefile = new File(baseUrl+filePath, fileName);
if (!savefile.exists()) {
if (!savefile.getParentFile().exists()) { // 判断目标文件所在的目录是否存在
// 如果目标文件所在的文件夹不存在,则创建父文件夹
savefile.getParentFile().mkdirs();
if (!savefile.getParentFile().mkdirs()) {// 判断创建目录是否成功
System.out.println("创建目标文件所在的目录失败!");
}
}
savefile.createNewFile();
}
String paths=FileUtils.getFile(savefile).toString().replaceAll("\\\\", "/");// 转移符 "\"变成"/"
FileUtils.copyInputStreamToFile(file.getInputStream(), savefile);//保存文件
Index index=new Index();
index.setIndexId(requestMap.get("indexId").toString());
index.setRemark(paths);
int records=indexMapper.updateByPrimaryKeySelective(index);// 更新数据库地址
if(records>0){
json.setObj(index);
json.setSuccess(true);
json.setCode("200");
json.setMsg("上传成功");
}else {
json.setMsg("无更新记录");
json.setCode("300");
json.setSuccess(false);
}
return json;
}
/**
* 方法名:uploadPhoto
* 描 述:TODO(上传图片)
* 作 者:池彦龙
* 时 间:2017/04/30
* 返回类型:
* 参 数:
* 异 常:
*/
mapper 层:
public interface IndexMapper {
int updateByPrimaryKeySelective(Index record);
}
/**
* 方法名:uploadPhoto
* 描 述:TODO(上传图片)
* 作 者:池彦龙
* 时 间:2017/04/30
* 返回类型:
* 参 数:
* 异 常:
*/
XML 层:
<update id="updateByPrimaryKeySelective" parameterType="com.xch.sysManager.model.yfzs.Index">
update YFZS.XHC_TAB_SC_INDEX
<set> <if test="upIndexId != null">
UP_INDEX_ID = #{upIndexId,jdbcType=VARCHAR},
</if>
<if test="remark != null">
REMARK = #{remark,jdbcType=VARCHAR},
</if>
</set>
where INDEX_ID = #{indexId,jdbcType=VARCHAR}
SpringMVC+MyBaties关于上传(跟新)图片的问题的更多相关文章
- SpringMVC上传文件(图片)并保存到本地
SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multi ...
- SpringMVC学习--文件上传
简介 文件上传是web开发中常见的需求之一,springMVC将文件上传进行了集成,可以方便快捷的进行开发. springmvc中对多部件类型解析 在 页面form中提交enctype="m ...
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- SSM框架-SpringMVC 实例文件上传下载
一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...
- [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载
一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...
- SpringMvc(四)- 下载,上传,拦截器
1.图片下载 图片下载:将服务器端的文件以流的形式写到客户端,通过浏览器保存到本地,实现下载: 1.1 图片下载步骤 1.通过session获取上下文对象(session.getServletCont ...
- dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.
http://wxb.github.io/dropzonejs.com.zh-CN/dropzonezh-CN/ 由于项目需要,完成一个web的图片拖拽上传,也就顺便学习和了解了一下前端的比较新的技术 ...
- ruby -- 进阶学习(四)paperclip上传中文命名图片
Paperclip -- 上传中文命名图片 使用Paperclip和ImageMagick插件来处理图片的时候,上传非中文命名的图片时,只要把配置写好就没问题 if you need to ...
- springMVC+jsp+ajax上传文件
工作中遇到的小问题,做个笔记 实现springMVC + jsp + ajax 上传文件 HTML <body> <form id="myform" method ...
随机推荐
- 09Oracle Database 数据表数据插入,更新,删除
Oracle Database 数据表数据插入,更新,删除 插入数据 Insert into table_name(column) values(values); insert into studen ...
- springmvc学习及源码地址
http://jinnianshilongnian.iteye.com/blog/1634096
- input file 美化的方法
css input[type=file] 样式美化,input上传按钮美化 2014年8月29日 113210次浏览 由于明天公司组织出去游玩,今天把这两天的博客都写了吧,今天的内容是input[ty ...
- C++STL快速入门学习
C++ STL中最基本以及最常用的类或容器无非就是以下几个: string vector set list map 下面就依次介绍一下它们,并给出一些最常见的使用方法,做到最快入门. string 首 ...
- circumferential averaged streamwise velocity in ParaView
Goal: get a averaged axial velocity in a circular loop (dashed line in the following figure) Steps: ...
- Hashing - Hard Version
Hashing - Hard Version Given a hash table of size N, we can define a hash function . Suppose that th ...
- BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)
题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604 (bzoj) https://www.lydsy.com/JudgeOnline/p ...
- mongodb shell之使用js(二)
mongodb shell之使用js(二) mongodb shell不仅是个交互式shell,还能够使用js脚本进行访问. 使用js脚本进行交互的优点与缺点 (1)无需任何驱动或语言支持: (2)方 ...
- [luoguP1029] 最大公约数和最小公倍数问题(数论)
传送门 一.暴力枚举(加了点优化) #include <cstdio> int x, y, ans; inline int gcd(int x, int y) { return !y ? ...
- Self Centos + Windows server 2016
Set up by Derek: 2019-1-25 登陆个人物理机: license 60天Free , 如果过期,就在 VMware ESXI 6.5.0的黑屏界面去reset. https:/ ...