美文网首页
SpringMVC图片上传

SpringMVC图片上传

作者: 海棠晴yyh | 来源:发表于2018-10-25 09:04 被阅读0次

    此方法是别人写的(已跑通),留作参考。

    pom.xml:

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

    spring-mvc.xml:

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
        <bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="UTF-8" />
            <!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
            <property name="maxUploadSize" value="10240000" />
        </bean>
    

    index.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>单图片上传</title>
    </head>
    <body>
    <fieldset>
        <legend>图片上传</legend>
        <h2>只能上传单张10M以下的 PNG、JPG、GIF 格式的图片</h2>
        <form action="/user/photoUpload" method="post" enctype="multipart/form-data">
            选择文件:<input type="file" name="file">
            <input type="submit" value="上传">
        </form>
    </fieldset>
    </body>
    </html>
    

    Controller.java:

    /**
         * 图片文件上传
         */
        @RequestMapping( "/photoUpload")
        public ResultData<Object> photoUpload(MultipartFile file, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IllegalStateException, IOException {
            System.out.println("!1111111111");
            ResultData<Object> resultData=new ResultData<Object>();
            // 判断用户是否登录
            /*User user=(User) session.getAttribute("user");
            if (user==null) {
                resultData.setCode(40029);
                resultData.setMsg("用户未登录");
                return resultData;
            }*/
            if (file!=null) {// 判断上传的文件是否为空
                String path=null;// 文件路径
                String type=null;// 文件类型
                String fileName=file.getOriginalFilename();// 文件原名称
                System.out.println("上传的文件原名称:"+fileName);
                // 判断文件类型
                type=fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
                if (type!=null) {// 判断文件类型是否为空
                    if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) {
                        // 项目在容器中实际发布运行的根路径
                        String realPath=request.getSession().getServletContext().getRealPath("/");
                        // 自定义的文件名称
                        String trueFileName=String.valueOf(System.currentTimeMillis())+fileName;
                        // 设置存放图片文件的路径
                        path=realPath+/*System.getProperty("file.separator")+*/trueFileName;
                        System.out.println("存放图片文件的路径:"+path);
                        // 转存文件到指定的路径
                        file.transferTo(new File(path));
                        System.out.println("文件成功上传到指定目录下");
                    }else {
                        System.out.println("不是我们想要的文件类型,请按要求重新上传");
                        return null;
                    }
                }else {
                    System.out.println("文件类型为空");
                    return null;
                }
            }else {
                System.out.println("没有找到相对应的文件");
                return null;
            }
            return resultData;
        }
    

    ResultData.java:

    public class ResultData<T> {
        private T data;
    
        private int code =200;
    
        private String msg;
    
        private Boolean success = true;
        public Boolean getSuccess() {
            return success;
        }
        public void setSuccess(Boolean success) {
            this.success = success;
        }
        public T getData() {
            return data;
        }
        public void setData(T data) {
            this.data = data;
        }
        public int getCode() {
    
            return code;
        }
        public void setCode(int code) {
            if(200 != code){
                success = false;
            }
            this.code = code;
        }
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    

    相关文章

      网友评论

          本文标题:SpringMVC图片上传

          本文链接:https://www.haomeiwen.com/subject/tdfqtqtx.html