基于maven的springmvc项目图片上传

发布于:2022-12-29 ⋅ 阅读:(476) ⋅ 点赞:(0)

在这里插入图片描述

配置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;
    }
}

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


网站公告

今日签到

点亮在社区的每一天
去签到