zoukankan      html  css  js  c++  java
  • 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}
  • 相关阅读:
    mixin混合
    python类内部调用自己的成员函数必须加self
    合并/取消合并单元格
    pandas 显示所有的行和列
    pandas 利用openpyxl设置表格样式
    Nowcoder9983G.糖果(并查集)
    Nowcoder9983F.匹配串(思维)
    Nowcoder9983E.买礼物(链表+线段树)
    Nowcoder9983C.重力追击(暴力+少用sqrt)
    Nowcoder9983B.内卷(双指针)
  • 原文地址:https://www.cnblogs.com/cyl048/p/6670093.html
Copyright © 2011-2022 走看看