/**
* 方法名: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}

最新文章

  1. mysql导入导出.csv格式数据
  2. Android开发--Adapter的应用
  3. 为WPF和Silverlight的Grid添加边框线(zz)
  4. 从零开始学ios开发(十六):Navigation Controllers and Table Views(下)
  5. 使用DNSSCrypt解决DNS污染问题
  6. 剑指OFFER之从上往下打印二叉树(九度OJ1523)
  7. EF中使用语句 或存储过程 查询(转)
  8. MVC---Case 1
  9. DB2 VC++ 中连接字符串
  10. 老李分享:《Linux Shell脚本攻略》 要点(二)
  11. 最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)
  12. 类String 初识
  13. win32gui.Findwindow(parm1,parm2)查找窗口的句柄方法
  14. Oracle Initialzation error instantclient_11_2 32位客户端问题
  15. hibernate经常报的几个不起眼的错误, 都是因为不细心或者手滑了输入有误造成了
  16. hdu1251 && hud 1247 (字典树)
  17. mybatis之Sql语句构建器
  18. 开发简单的IO多路复用web框架
  19. 170525、解决maven隐式依赖包版本问题
  20. jquery删除记录弹出提示框

热门文章

  1. 17Web应用乱码问题
  2. 第二节:1_C#中的委托的使用和讲解(转)
  3. 手机端h5复制功能
  4. 洛谷——P1342 请柬
  5. Linux命令学习(2): scp和rsync基本用法与断点续传
  6. 67.基于nested object实现博客与评论嵌套关系
  7. How Can You Tell the Difference Between LINQ Methods and Query Builder Methods?
  8. BNUOJ 2461 Anniversary party
  9. HDU 1160 排序或者通过最短路两种方法解决
  10. noip模拟赛 abcd