springMvc 文件上传

来源:互联网 发布:用友网络2017最新消息 编辑:程序博客网 时间:2024/04/30 13:17

xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 文件上传 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 以字节为单位的最大上传文件的大小 -->
        <property name="maxUploadSize" value="3145728" />
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>
</beans>


java代码如下:

/*****

     * 修改用户头像 将文件上传请求映射到该方法
     *
     * @param request
     * @param 用户id
     * @param mFile
     *            上传文件
     * @return
     */
    @RequestMapping(value = "/modPTT/0", method = RequestMethod.POST)
    @ResponseBody
    public Object handleFormUpload(
            @RequestHeader("saikeMobileHead") Map<String, String> headMap,
            @RequestParam("userId") Integer id,// 设置请求参数的名称和类型
            @RequestParam("uploadfile") CommonsMultipartFile mFile) { // 请求参数一定要与form中的参数名对应
        logger.info("saikeMobileHead:" + JSON.toJSONString(headMap));
        logger.info("/modPTT/0");
        logger.info("request:" + id + ":" + JSON.toJSONString(mFile));
        if (mFile.isEmpty()) {
            return ErrorInfoUtil.setErrorInfo(
                    ErrorCodeConsField.ERROR_MSG_10001, propertiesUtil
                            .getMessage(ErrorCodeConsField.ERROR_MSG_10001)); // "redirect:uploadFailure";返回失败视图
        }
        // String url=mFile.getFileItem().getName();
        // String path = "/opt/apache-tomcat-7.0.47/bartackserver/";

        String imgurl = id + "_" + DateUtil.getNowDate() + ".jpg";
        String pttUrl = propertiesUtil.getMessage(ConsField.IMAGE_UPLOAD_URL)
                + imgurl;// "/PTT/"+
        // MD5Util.getMD5(new
        // Date().getTime()+"")+
        // ".jpg";//设置存储路径
        try {
            File file = new File(pttUrl);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            mFile.getFileItem().write(file); // 将上传的文件写入新建的文件中

        } catch (Exception e) {
            logger.error("头像文件上传出错" + e.getMessage());
            e.printStackTrace();
            return ErrorInfoUtil.setErrorInfo(
                    ErrorCodeConsField.ERROR_MSG_10002, propertiesUtil
                            .getMessage(ErrorCodeConsField.ERROR_MSG_10002));
        }

        logger.info("头像文件写入成功");
        User user = new User();
        user.setId(Integer.valueOf(id));
        // + mFile.getFileItem().getName();// 设置读图片路径
        // propertiesUtil.getMessage("image.load.url")
        user.setPttUrl(imgurl);
        boolean flag = this.userDAO.updatePtt(user);
        if (flag) {
            return ResultInfoUtil.setSuccessInfo(propertiesUtil
                    .getMessage("image.http.url")
                    + propertiesUtil.getMessage("image.load.url") + imgurl);// "redirect:uploadSuccess"返回成功信息
        }
        logger.error("头像写入错误:" + id);
        return ErrorInfoUtil.setErrorInfo(ErrorCodeConsField.ERROR_MSG_10002,
                propertiesUtil.getMessage(ErrorCodeConsField.ERROR_MSG_10002));

    }
0 0
原创粉丝点击