基于maven的springmvc项目图片上传

在这里插入图片描述

配置pom.xml文件图片上传依赖:

<dependency>
		<groupId>commons-fileupload</groupId>
		<artifactId>commons-fileupload</artifactId>
		<version>1.3.3</version>
	</dependency>
	<dependency>
		<groupId>commons-io</groupId>
		<artifactId>commons-io</artifactId>
		<version>2.6</version>
	</dependency>

配置springMVC-servlet.xml文件图片上传

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

JSP文件
标签:enctype属性规定在发送到服务器之前应该如何对表单数据进行编码

<form id="user-form" method="post"  enctype="multipart/form-data">
...
 <div class="fitem">
            <label>头像:</label>
           <input class="easyui-filebox" name="file" data-options="buttonText:'选择文件',buttonAlign:'left'"  style="width:100px">
        </div>
...        
</form>

控制器中的写法

     @RequestMapping(params = "act=edit")
    @ResponseBody
    public Map edit(User user, @RequestParam(value = "photo") MultipartFile file, HttpServletRequest request){

            String filePath = request.getServletContext().getRealPath("/")+"/resources/upload/";
            String path="";

            try {
                if(null!=file) {
                    path = UploadUtil.fileUpload(file, filePath);
                }
            }catch (IOException e){
                return MessageUtil.getErrorResult("上传出错");
            }
            System.out.println(request.getServletContext().getContextPath()+"/resources/upload/"+path);

            if(!StringUtils.isEmpty(path)){
                user.setFile(request.getServletContext().getContextPath()+"/resources/upload/"+path);
            }
            if(null== user.getId() ){
                userService.addUser(user);
            }else{
                userService.updateUser(user);
            }
            return MessageUtil.getSuccessResult("编辑成功");
    }

UploadUtil.java

package ssm.util;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class UploadUtil {

    /**
     * 文件上传
     *
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public static String fileUpload(MultipartFile multipartFile, String filePath) throws IOException {
        // 判断上传的文件是否为空
        if (multipartFile != null) {

            // 判断文件大小
            if (multipartFile.getSize() >= (50 * 1024 * 1024)) {
               //info("抱歉,仅支持50M以内的文件上传:(");
               return null;
            }
            //文件原名称
            String fileName = multipartFile.getOriginalFilename();
            // 判断文件类型
            String fileType = fileName != null && fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            // 判断文件类型是否为空
            if (!StringUtils.isEmpty(fileType)) {
                // 自定义的文件名称
                fileName = fileName.substring(0, fileName.lastIndexOf("."));
                String trueFileName = fileName + "_" + String.valueOf(System.currentTimeMillis()) + "." + fileType;
                // 设置存放图片文件的路径
                filePath += trueFileName;
                // 转存文件到指定的路径
                File file = new File(filePath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                multipartFile.transferTo(file);
               // log.info("上传成功");
                return trueFileName;
            } else {
               // log.info("文件类型为空");
                return null;
            }
        } else {
            //log.info("没有找到相对应的文件");

        }
        return null;
    }
}

这里面的逻辑可以根据实际需要修改
在这里插入图片描述
我的文件已经上传到这里了。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

reg183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值